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
JNicJNic 

test class - attempt to de-reference a null object

Hey guys... I'm geting really frustrated with having to test my classes... but *sigh* SF makes me do it anyways... Here's my problem:

 

I'm trying to test a page reference method that updates child records.

 

 

I've done the following:

 

It saves correctly, but when I run the test I get: 

System.NullPointerException: Attempt to de-reference a null object

 



//Get back to the imaction Manager
PageReference refreshAxnMgr = new PageReference('/apex/IMACtionManager?id=' + thisImc.id);
Test.setCurrentPage(refreshAxnMgr);

//Instantiate and construct the controller class.
ApexPages.StandardController imcCon1 = new ApexPages.StandardController(new IMAC__c());
//extend controller with extension, referencing base controller
ImacController imcExt1 = new ImacController(imcCon1);


//Simulate the {!saveAXNS} expression in the Visualforce page
// by directly calling the saveAXNS method.
String saveAXNS = imcExt1.saveAXNS().getUrl(); <--dereferencing a null object (whatever that means)

 

//It should be back at the manager
System.assertEquals('/apex/IMACtionManager?id=' + thisImc.id, saveAXNS);

 

Here is the method Actual:

 

//Save function for IMACtions - used on IMACtions pageblock public PageReference saveAXNS() { try { update actions; } catch (Exception e) { ApexPages.addMessages(e); } return null; }

 

 

Thanks,

JN

 

 

Best Answer chosen by Admin (Salesforce Developers) 
richardvrichardv

Change your code to:

 

 //Simulate the {!saveAXNS} expression in the Visualforce page
// by directly calling the saveAXNS method.
PageReference saveAXNS = imcExt1.saveAXNS(); <--dereferencing a null object (whatever that means)

//It should be back at the manager
System.assertEquals(null, saveAXNS);

All Answers

richardvrichardv
saveANXS() returns null so basically the offending line is attempting to execute null.getUrl();  You can't call a method on null - that's why a null pointer exception is being thrown.
JNicJNic
Thanks Richard. How can I create a test for saveAXNS then?
richardvrichardv

Change your code to:

 

 //Simulate the {!saveAXNS} expression in the Visualforce page
// by directly calling the saveAXNS method.
PageReference saveAXNS = imcExt1.saveAXNS(); <--dereferencing a null object (whatever that means)

//It should be back at the manager
System.assertEquals(null, saveAXNS);

This was selected as the best answer
JNicJNic

Ahh... Thanks!