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
anuragsinghnsanuragsinghns 

Attempt to dereference a null object error

Hi Guys,
I get Attempt to dereference a null object error with the system.debug below but if I comment it out the case gets created and also an account gets associated with it!Could you help me resolve this issue

//associating a account to the case
caseAccountPresent.accountId=accountUniqueIdMap.get(caseUniqueIdMap.get(caseAccountPresent)).Id; 
 //debug of the same
system.debug('the Account'+accountUniqueIdMap.get(caseUniqueIdMap.get(caseAccountPresent)).Id);
Best Answer chosen by anuragsinghns
logontokartiklogontokartik
Hi Anurag,
 
Looks like you are using an SOBject as a key in your map. thats not really recommended.  And your code is changing that SOBject in the line

caseAccountPresent.accountId=accountUniqueIdMap.get(caseUniqueIdMap.get(caseAccountPresent)).Id;

and in the System.debug, when you are trying to get the value from map, since SObject is changed, its not finding the value throwing NPE. 

Hope this helps.


All Answers

logontokartiklogontokartik
Hi Anurag,
 
Looks like you are using an SOBject as a key in your map. thats not really recommended.  And your code is changing that SOBject in the line

caseAccountPresent.accountId=accountUniqueIdMap.get(caseUniqueIdMap.get(caseAccountPresent)).Id;

and in the System.debug, when you are trying to get the value from map, since SObject is changed, its not finding the value throwing NPE. 

Hope this helps.


This was selected as the best answer
anuragsinghnsanuragsinghns
@logontokartiklogontokartik I think you hit the bull's eye with that one :)
sorry stupid question come to think about it.
Thank you very much