+ Start a Discussion
Scott M - SFDC FanScott M - SFDC Fan 

How to troubleshoot null pointer exceptions


This is a general question. I'm not an experienced SF developer and I am running a test and it says I am trying to dereference a null object. I've set a checkpoint on the line under the line that is causing the error and from what I can tell, the object in question isn't null.

What else can I do to troubleshoot this?

Akhil ReddyAkhil Reddy
Just to make sure it variable is not null, use few checks before using it in logic. What I whould do is,
1. Initiate the variable when declaringing it.
List<Sobject> SO = new List<Sobject>()

2. Check the variable if it is null or empty. Most of dereference a null object error comes with this case, we try to access collections which are empty
if (SO.size()>0){
// logic


if (map.getvalu(key) != NULL){

Scott M - SFDC FanScott M - SFDC Fan

Hey Akhil. Thanks for the quick response.

Your suggestion is how to code to avoid null pointer exceptions. From all I can tell, I don't have an issue.

The the line of code that is saying I am attempting to de-reference a null object:


if(caseToSync.theCase.SAP_Status__c == 'Modify failed' ){
And looking at the break point just after that line, the variable is also set.

User-added image

What else can I do to figure out what is breaking?

Naval Sharma4Naval Sharma4
Did you check your test data which you are creating in the test class?
Akhil ReddyAkhil Reddy
I believe you have tried system.debug statement before the if condition. and Also please add if (caseToSync.theCase.SAP_Status__c != null || caseToSync.theCase.SAP_Status__c != ' ')statement to check it is assigned with some value. What I am trying to get it from this is to see if breaker is check at correct place of execution. Some times we miss it.