When we use dataloader to upload records into database we have got the null pointer error when we use the related salesforce Id. But it works when we switch to use external Id.

I am just puzzled to find out the reason.

Below is my related trigger code:

    List<String> relatedOlIdList = new List<String>();
    for(UK_Job__c job : (List<UK_Job__c>)Trigger.New) 
    relatedOlMap = new Map<Id, UK_Order_Line__c>([Select    Id, Name, 
                                                    From    UK_Order_Line__c
                                                    Where   Id in :relatedOlIdList]);
        UK_Order_Line__c relatedOl = relatedOlMap.get(job.UK_Order_Line__c);
        if(relatedOl.Transcript_Due__c == null) // This is the line getting null pointer reference error

I have double checked that in the .csv file, the related UK_Order_Line__c Id is valid and does exist in the system. And if we switch into external Id it just works.

But it is really weird that it can't find the relatedOl in this case.

Both code are executed both in before insert and before update.

  • what if job.UK_Order_Line__c is null? – kurunve Jun 5 '18 at 7:49
  • For situations like this, I normally just write if(relatedOl != null && relatedOl.Transcript_Due__c == null)... or use a Map.containsKey() call – Caspar Harmer Jun 5 '18 at 7:50
  • @kurunve it can't be null. It is a master-detail relationship and I have checked some Ids. – Lance Shi Jun 5 '18 at 8:00

From the code that you have provided I see that on row

UK_Order_Line__c relatedOl = relatedOlMap.get(job.UK_Order_Line__c);

the variable job is not defined, since you are outside of loop for. That might be the issue for you, unless you haven't provided the complete code..

  • It is not a compile error. – Lance Shi Jun 5 '18 at 9:25
  • @LanceShi now that I see, you are accessing: job.UK_Order_Line__c. From the code I see that UK_Order_Line__c is the name of the custom object, right? Or do you have it also as a custom field? If so, you have to include it in your query. – Enrik Hysko Jun 5 '18 at 9:42
  • The code compiles and works for most cases. So it shouldn't be something like this. – Lance Shi Jun 5 '18 at 9:47

it will give null pointer if you do not have key in this map relatedOlMap.

use this to avoid errors.

    UK_Order_Line__c relatedOl = relatedOlMap.get(job.UK_Order_Line__c);

Hope this helps :)

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.