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

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
Try:
at line 70
All Answers
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;
}
Thanks, but this led to a compile error:
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);
Try:
at line 70
Yes, it should be
atm= new AccountTeamMember();
I forgot to mention that in the code. Anyway apologies for the mistake.
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
Hey Pete,
You don't really need the first line; you could just do this instead: