+ Start a Discussion
ckellieckellie 

Attempting to dereference a null object

I am trying to add-on to one of my existing triggers and am running into the following error:

 

 

Error:Apex trigger triggerSendLayoutTask caused an unexpected exception, contact your administrator: triggerSendLayoutTask: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.triggerSendLayoutTask: line 20, column 46

 

 

Here is the code I am editing:

 

 

    List<Custom_Layout_Object__c> gen = [Select id, ready_to_submit_task__c from Custom_Layout_Object__c limit 1];
    Set<Id> gen0 = new Set<Id>();
        Map<Id, user> userMap1 = new Map<Id, user>([select id, Region__c from user where id = :UserInfo.getUserId() and isActive = true]);
       for(Custom_Layout_Object__c l2: trigger.new ){
            if (userMap1.get(l2.createdById).Region__c == 'Europe' && l2.ready_to_submit_task__c == 'Items are Attached'){
           User[] userid = [Select id from User where Name = 'Jasper Schoemaker' ];
               Task ta = new Task(whatID =trigger.new[0].Opportunity__c,ActivityDate=trigger.new[0].Requested_Date_To_Complete_Layout__c,
               subject='Lay-out Requested', ownerid=userid[0].id);
       Database.DMLOptions dmlo = new Database.DMLOptions();
         dmlo.EmailHeader.triggerUserEmail = true;
         database.insert(ta, dmlo);
       Tasks.add(ta);}

 

 

The line I have edited is:

 

            if (userMap1.get(l2.createdById).Region__c == 'Europe' && l2.ready_to_submit_task__c == 'Items are Attached'){

 

I know I need to declare the new field "l2.ready_to_submit_task__c, but am not remembering exactly how in this situation.

 How do I do this?

 

Thank you

 

2.ready_to_submit_task__c 
forecast_is_cloudyforecast_is_cloudy

I don't think your problem is with the 'l2.ready_to_submit_task__c' part of that condition statement. Its with the first half of the If statement. Your  'userMap1' Map variable must be returning a null value and you're then trying to access the 'Region__c' field on the null variable (hence the null pointer exception). Try this as your If statement:

 

if (userMap1.get(l2.createdById) != null && userMap1.get(l2.createdById).Region__c == 'Europe' && l2.ready_to_submit_task__c == 'Items are Attached')
ericszulcericszulc

Looks to me that l2.createdById might not be in userMap1. That will return null, and then you try to pull Region__c from a null object.

 

Maybe try?

 

 

if(userMap1.get(12.createdById))
{
    if(userMap1.get(l2.createdById).Region__c == 'Europe' && l2.ready_to_submit_task__c == 'Items are Attached')
    {
        ...
    }
}