+ Start a Discussion
huskerwendyhuskerwendy 

Trigger Null Pointer Error

I'm new to Salesforce and Apex coding. I'm trying to create a trigger and am getting a null pointer error.

 

I have a custom object, VD_Documents, that has a lookup field to the Account object. I'm trying to update the Account Number field on the custom object with the Account Number field from the Account object when a VD_Document is inserted or updated.

 

I've found some code online that I'm trying to modify to work for me, but am receiving a null pointer exception. Here's my trigger code:

 

trigger UpdateAccountNumber on VD_Documents__c (before insert, before update) {
    // create a set of all the unique accounts
    List<id> accountIds = new List<id>();
    for (VD_Documents__c a : Trigger.new)
        accountIds.add(a.Account__r.id);   
 
    // create a map for a lookup / hash table for the account info
    Map<id, Account> accounts = new Map<id, Account>([Select id, AccountNumber from Account Where Id in:accountIds]);  
 
    // iterate over the list of records being processed in the trigger and set the AccountNumber
    for (VD_Documents__c a : Trigger.new)
        a.Account_Number__c = accounts.get(a.Account__r.Id).AccountNumber;
}

 

The error message is:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateAccountNumber caused an unexpected exception, contact your administrator: UpdateAccountNumber: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateAccountNumber: line 12, column 61

 

I get the same error when I try to insert or update a record. I've also tried changing the trigger to After Insert and After Update. I would appreciate any help you can give me.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu

You can make use of system.debug statement to print them in the debug logs.

 

Intial query is correct, verify whether you have select the account if yes whether selected account is having a valid account number or not. If there is no account number then it will not populate.

 

Below query is correct

Map<id, Account> accounts = new Map<id, Account>([Select AccountNumber from Account Where Id in:accountIds]);

All Answers

hisrinuhisrinu

before to the execution of the below line 12 you need to do a check whether that record is avaible in the map or not.

 

       If(accounts.containsKey(a.Account__r.Id))

        a.Account_Number__c = accounts.get(a.Account__r.Id).AccountNumber;

huskerwendyhuskerwendy

Thanks, Srini. That got rid of my error, but it's not populating the value in the Account Number field.

 

I think the problem is that I'm not actually getting the AccountNumber when I populate the map. I have to admit that I don't totally understand what that is doing. I copied it and modified it to work for me. This is the part that I'm confused about.

 

Map<id, Account> accounts = new Map<id, Account>([Select AccountNumber from Account Where Id in:accountIds]); 

 

I can change the query to say ([Select id from Account where id in : accountIDs])  so I'm not quite sure what is being populated in the map. Shouldn't it be the account object id and the account object itself?

 

Is there a way to print out what is in my List and Map so that I can see what the values are?

 

Thanks for your help.

hisrinuhisrinu

You can make use of system.debug statement to print them in the debug logs.

 

Intial query is correct, verify whether you have select the account if yes whether selected account is having a valid account number or not. If there is no account number then it will not populate.

 

Below query is correct

Map<id, Account> accounts = new Map<id, Account>([Select AccountNumber from Account Where Id in:accountIds]);

This was selected as the best answer
huskerwendyhuskerwendy

Srini,

 

Thanks so much for your help! I got it working once I could see my debug statements.

 

I really appreciate all your help and your quick responses to my questions.

 

Wendy