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
pjaenickepjaenicke 

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

Greetings listers,

 

I'm encountering the infamous "AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object" message....  line 70, column 13, here's my code snippet where I'm running into the problem:

 

        AccountTeamMember atm;
        AccountTeamMember[] atmsToInsert = new AccountTeamMember[0];
        User u;
        index=0;
        for(Account a : accts) {
            if (a.OwnerId == Null) system.assertequals(1,2);
//            system.assertequals(3,4);
            atm.UserId = a.OwnerId;                          //this is line 70, where the error is occurring.

 

What am I missing?  Thanks for any assistance.  (a.OwnerId successfully holds the account owner Id).

 

Pete

Best Answer chosen by Admin (Salesforce Developers) 
uptime_andrewuptime_andrew

Try:

 

 

atm = new AccountTeamMember()

 

at line 70

 

All Answers

Imran MohammedImran Mohammed

Hi,

 

 AccountTeamMember atm;
        AccountTeamMember[] atmsToInsert = new AccountTeamMember[0]; 
        User u;
        index=0;
        for(Account a : accts) {
            if (a.OwnerId == Null) system.assertequals(1,2);
//            system.assertequals(3,4);

 

 atm =  AccountTeamMember();//add this to your code
            atm.UserId = a.OwnerId;    

}

pjaenickepjaenicke

Thanks, but this led to a compile error:

Error: Compile Error: Method does not exist or incorrect signature: AccountTeamMember() at line 70 column 17 

 

 

        AccountTeamMember atm;
        AccountTeamMember[] atmsToInsert = new AccountTeamMember[0];
        User u;
//        string OwnerId;
        index=0;
        for(Account a : accts) {
            if (a.OwnerId == Null) system.assertequals(1,2);
//            system.assertequals(3,4);
            atm=AccountTeamMember();//add this to your code -- (this is line 70)
            atm.UserId = a.OwnerId;
            atm.TeamMemberRole = 'Sales Rep';
            atmsToInsert.add(atm);

uptime_andrewuptime_andrew

Try:

 

 

atm = new AccountTeamMember()

 

at line 70

 

This was selected as the best answer
Imran MohammedImran Mohammed

Yes, it should be 

atm= new AccountTeamMember();

 

I forgot to mention that in the code. Anyway apologies for the mistake.

pjaenickepjaenicke

Imram / Andrew - excellent !  Worked like a charm - thank you kindly !

 

So that I can understand this better - why do I need -both- of these declarations? 

 

  AccountMember atm;

  atm= new AccountTeamMember();

 

Thanks again,

Pete

 

 

uptime_andrewuptime_andrew

Hey Pete,

 

You don't really need the first line;  you could just do this instead:

 

 

AccountMember atm = new AccountTeamMember();