function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
G2WIntegrationG2WIntegration 

Standard Controller Test won't run: Argument 1 cannot be null

After wasting 1.5hrs, I can't get a standard controller test to even run, let alone pass, as the test thinks the campaign var in the controller is set to null, which hints that the constructor isn't running, but I can't figure out why.  I tried manually assigning a campaign to the controller var, but that didn't work either.

 

Anyone have any ideas?

 

Page

<apex:page standardController="Campaign" extensions="g2wPollChart1Controller">
    <apex:image url="{!chartData}"></apex:image>
</apex:page>

 

Controller (fails with getChartData calling g2wUtils.pollChartQuerySQL as the test thinks the campaign is null)

public with sharing class g2wPollChart2Controller {
    private String chartData;
//    private String chartTitle;
    private Integer pollNum;    

    private Campaign camp;
    
    public g2wPollChart2Controller (ApexPages.StandardController stdController) { 
        pollNum=2;//This is the only thing that changes between the 5 chart controllers
        
        this.camp = (Campaign)stdController.getRecord();  
        String query=g2wUtils.pollChartQuerySql(pollNum, camp.Id);
        camp=database.query(query);
    }//g2wPollChart1Controller Constructor
    
    public String getChartData(){
	    Integer width=500;
	    Integer height=200;
	    String chartData=g2wUtils.pollChartURL(camp, pollNum, width, height);
	    return chartData;                
  }//getChartData

}//g2wPollChart1Controller

 

Test

static testMethod void verifyPollChartControllers(){
//Bunch of code to create a campaign with the right data
//...
//Then the heart of the test
		PageReference pageRef=Page.g2wPollChart1;
		Test.setCurrentPage(pageRef);
		ApexPages.StandardController sc=new ApexPages.StandardController(campaigns[0]);
				
        	g2wPollChart1Controller cont = new g2wPollChart1Controller(sc);

//This is where the Error occurs:
	String getChartData=cont.getChartData();

    }

 

Best Answer chosen by Admin (Salesforce Developers) 
G2WIntegrationG2WIntegration

Thank you both for your responses!  Turns out Apex was pointing at the wrong place - the error wasn't on the line it said it was, and all of the data was flowing back and forth fine.

 

The root cause was I tried to do String.Contains(var), where var = null.  

 

This was 20 lines below where the error said the "argument 1 can't be found" though, which made this super frustrating.

 

Bob - your idea of logging everything helped finally work through it and aballard, your idea of adding the query details helped as well as it fed the log lines. 

 

 

All Answers

bob_buzzardbob_buzzard

Your test isn't using the code that you have posted - the page extension and test are using g2wPollChart1Controller, while the controller code posted is g2wPollChart2Controller.  Based on your comments I'll assume that isn't a problem.

 

Have you tried adding debug statements to the controller to check that the constructor is progressing as you expect?

 

aballardaballard

You constructor first sets camp to the campaign retrieved from the standardController, then overrwrites it with the result of the query.   I assume the query is not finding anything.   How did you create the testData in the  Campaigns[0]?  Is it in the database?

G2WIntegrationG2WIntegration

Thank you both for your responses!  Turns out Apex was pointing at the wrong place - the error wasn't on the line it said it was, and all of the data was flowing back and forth fine.

 

The root cause was I tried to do String.Contains(var), where var = null.  

 

This was 20 lines below where the error said the "argument 1 can't be found" though, which made this super frustrating.

 

Bob - your idea of logging everything helped finally work through it and aballard, your idea of adding the query details helped as well as it fed the log lines. 

 

 

This was selected as the best answer