You need to sign in to do that
Don't have an account?

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 | trigger changeLeadStatus on Task (before insert, before update) { |
t.whold is null,
and String.valueOf(t.whoId) is null too,
therefore it crashes when you try to do null.startsWith()
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.
Just an FYI about your code, change:
to
The '== True' portion is kind of redundant.
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.
You can use the new Id.getSObjectType() function to simplify your code:
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: