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


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){
     //---------------**Following IF is causing an error --------**
     if(objEmp.Location__c==null  || objEmp.Location__c!=null ?    
          //Create ThirdObjectRecord
          ThirdObject__c tobj = new ThirdObject__c();

ERROR IN MY DEBUGS on Employee record Emp1:

----EMP--1 Emp1 null -

FATAL_ERROR|System.NullPointerException: Argument cannot be null.


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

  • Yah !!!! thats the Silly mistake I was doing, sometimes you got caught in thousand line code and big issues and dont concentrace on minor things !! Thanks it worked. – Salesforce Steps Jan 23 '15 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.