The execution of a trigger will sometimes generate an error message like the following:
execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]
This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.
Example 1: If the get() method in the following code cannot find the value oldAccount.Name in the map accountMap, the object newAccount will be null, and trying to use it will generate this error message:
trigger SampleTrigger on Account (before insert, before update){ integer i = 0; Map<String, Account> accountMap = new Map<String, Account>{}; for (Account acct : System.Trigger.new) accountMap.put(acct.Name, acct); for (Account oldAccount : [SELECT Id, Name, Site FROM Account WHERE Name IN :accountMap.KeySet()]) { Account newAccount = accountMap.get(oldAccount.Name); i = newAccount.Site.length; } }
Good to know: If the field Site was left empty, trying to use it as above will generate the error message as well.
Resolution |
|
---|
No comments:
Post a Comment