You need to sign in to do that
Don't have an account?
Using Map: System.NullPointerException: Attempt to de-reference a null object
Not an expert with Apex, but I was assuming this should work.
My code is creating a record with values received thru a webservice call, I am trying to set the RecordType using one of the values I received. Code for setting recordtype looks something like this:
Map<String,RecordType> acctRecordTypes = new Map<String,RecordType>([select NAME from RecordType where SOBJECTTYPE='Account']);
...............................................................
if(nodeName=='AccountType') acctTypeCode=node.getText();
.....................................................
RecordTypeId=acctRecordTypes.get(acctTypeCode).ID,
Fails on the last line and gives an error message: "System.NullPointerException: Attempt to de-reference a null object"
Was able to isolate the problem to this line using debug as well, hardcoding the RecordTypeID value gives no errors.
Thanks for taking the time to read and any help you offer.
Unfortunately I believe that you can only build a map with a query that is ID=>Record. To do what you want you would have to build a second map that would map name=>record. It would look something like this:
That will waste execution lines and it may be easier to just grab the ID instead of the name, but I have not seen that code so I am unsure. Let me know if you have any questions.
All Answers
I would add these two lines before that last line to help you debug:
System.Debug(json.serializePretty(acctRecordTypes));
System.Debug("acctTypeCode======" + acctTypeCode);
What is happening is probably that your map does not contain an entry for the key you are passing, so when you do .ID you are trying to get data from NULL and throwing this error. Those debug statements will tell you if you are passing a bad key or if your query is returning enexpected data.
Jake, Thanks a bunch for taking the time to answer.
You are right, my map does not contain the key I passed in "acctTypeCode". I was under the impression, it was storing the Name value as Key, but it is storing the record ID as Key.
Here is what my map contains when I debugged:
{
"012Q00000000azQIAQ" : {
"attributes" : {
"type" : "RecordType",
"url" : "/services/data/v27.0/sobjects/RecordType/012Q00000000azQIAQ"
},
"Name" : "Franchise",
"Id" : "012Q00000000azQIAQ"
},
"012Q00000000azLIAQ" : {
"attributes" : {
"type" : "RecordType",
"url" : "/services/data/v27.0/sobjects/RecordType/012Q00000000azLIAQ"
},
"Name" : "Partner",
"Id" : "012Q00000000azLIAQ"
},
"012Q00000000azGIAQ" : {
"attributes" : {
"type" : "RecordType",
"url" : "/services/data/v27.0/sobjects/RecordType/012Q00000000azGIAQ"
},
"Name" : "ReSeller",
"Id" : "012Q00000000azGIAQ"
}
}
And I was trying to lookup the value .ID using the Key "Franchise". The map instead has the ID as a Key and not the Name as Key.
What should I do to make the Map value to the below format ?
"Franchise" : {
"attributes" : {
"type" : "RecordType",
"url" : "/services/data/v27.0/sobjects/RecordType/012Q00000000azQIAQ"
},
"Id" : "012Q00000000azQIAQ"
},
Unfortunately I believe that you can only build a map with a query that is ID=>Record. To do what you want you would have to build a second map that would map name=>record. It would look something like this:
That will waste execution lines and it may be easier to just grab the ID instead of the name, but I have not seen that code so I am unsure. Let me know if you have any questions.
Thanks Jake.
As you said I was trying to avoid execution lines and assumed the Selected columns will be created as Map key >> value pairs. Can't use ID as my input variable is a RecordType Name.
Anyway since I will only have one recordtype name as input, I will loop the recordtypes and grab the ID instead creating another map (or may be that is what you meant below).
Grateful for your quick response. Thanks again.