[SalesForce] Logical Help needed – FATAL_ERROR|System.NullPointerException: Argument cannot be null

I have List of Employee's, Now Want to create my ThirdObject record if

Condition 1) objEmp.Location__c ==null as well as If

Condition 2) objEmp.Location contain in objSecondObject.SecondLocation__c (multiselect picklist)

Example : Following Emp record

ID-Name-Location

1 – Emp1 – "" –

2 – Emp2 – INDIA –

SecondOBECT__C record

1 – CO1 – USA;INDIA –

2 – CO1 – USA

So , I want to create ThirdObject record

For Emp2 since its having match with SecondObject__C record no 1

as well as For Emp1 since its Location is =null/empty

 for(MyEmployee__c objEmp : lstEmp){
    System.Debug('----EMP--'+objEmp);
     //---------------**Following IF is causing an error --------**
     if(objEmp.Location__c==null  || objEmp.Location__c!=null ?    
        objSecond.SecondLocation__c.contains(objEmp.Location__c):FALSE)
      {
          //Create ThirdObjectRecord
          ThirdObject__c tobj = new ThirdObject__c();
          lstThirdObject.add(tobj);
      }
 }

ERROR IN MY DEBUGS on Employee record Emp1:

—-EMP–1 Emp1 null –

FATAL_ERROR|System.NullPointerException: Argument cannot be null.

Best Answer

The error is coming from this line:

if(objEmp.Location__c==null  || objEmp.Location__c!=null ?objSecond.SecondLocation__c.contains(objEmp.Location__c):FALSE)

As you can see in your debug logs, the Location__c field has a null value and so when you try to retrieve a value using it from the objSecond.SecondLocation__c map then it throws an error.

The underlying issue is your code is not separated out to allow the logic to parse properly. The system seems to be interpreting your line as:

if((objEmp.Location__c==null  || objEmp.Location__c!=null) ?objSecond.SecondLocation__c.contains(objEmp.Location__c):FALSE)

Rather than

if(objEmp.Location__c==null  || (objEmp.Location__c!=null objSecond.SecondLocation__c.contains(objEmp.Location__c):FALSE))

Obviously in the first example

(objEmp.Location__c==null  || objEmp.Location__c!=null)

always returns true (something is always null or not null). Try either adding the brackets in like I have to see if that helps resolve the issue or separate the logic out into a couple of lines.

I recorded a video as part of the @forcedotcomcast series a while back. May provide some more info for you http://youtu.be/6EFIe5aHgoM

Related Topic