function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luis GutierrezLuis Gutierrez 

System.NullPointerException: Attempt to de-reference a null object

I havent coded apex in a long time, this trigger was working for the past 8 months and then all the sudden we get this:

 

 

Apex script unhandled trigger exception by user/organization: 005U0000000fp0q/00DU0000000Kh8N

 

changeLeadStatus: execution of BeforeInsert

 

caused by: System.NullPointerException: Attempt to de-reference a null object

 

Trigger.changeLeadStatus: line 7, column 1

 

 

Any help would eb appricated

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Working - Contacted';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
    
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger

sdusdu

t.whold is null,

and String.valueOf(t.whoId) is null too,

therefore it crashes when you try to do null.startsWith()

 

Zach CarterZach Carter

Well obviously String.valueOf(...) would return null if the value it were operating on is null.

 

t.whold isn't necessarily null, the object t itself could be null.

 

Try using system.debug(t) to figure out whether it has been initialized or not. If it has, then check t.whold to make sure a value has been assigned to that property.

dphilldphill

Just an FYI about your code, change:

 

    if(String.valueOf(t.whoId).startsWith('00Q')==TRUE)

 to

    if(String.valueOf(t.whoId).startsWith('00Q'))

 The '== True' portion is kind of redundant.

 

Zach CarterZach Carter

And to explain ^ the String.startswith(...) function is going to return a boolean value (true / false).

 

So your conditional if block will evaluate to - if(true) or if(false) after making the correction dphill suggested above.

 

Otherwise you'd be evaluating the statement if(true == true) or if(false == true) thus the redundency.

sfdcfoxsfdcfox

You can use the new Id.getSObjectType() function to simplify your code:

 

if( t.whoid != null && t.whoid.getsobjecttype() == lead.sobjecttype ) {
// there is an id, and it is a lead ...
}

Note that you have to check if whoid is null first, or even string.valueof would return a null value, which would cause string.beginswith to fail.

 

You can actually even make it simpler than that, though; salesforce.com won't complain about an invalid ID in a condition clause:

 

trigger x on task( after insert, after update ) {
  map<id,lead> leads = new map<id,lead>();
  for( task t:trigger.new )
    if( t.status == 'Completed' )
      leads.put(t.whoid,null);
  leads = new map< id, lead >([select id from lead where id in :leads.keyset() and isconverted = false]);
  for(lead l:leads.values())
    l.status='Contacted - Working';
  SaveResult[] updates = database.update(leads.values(),false); // okay to ignore error?
  for( saveresult sr:updates )
    if( !sr.issuccess() )
      System.debug('*** ERROR: ' + sr.geterrors());
}