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
GravityGravity 

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.

 


 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

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:

 

 

Map<String,RecordType> acctRecordTypes = new Map<String,RecordType>([select NAME from RecordType where SOBJECTTYPE='Account']);

Map<String,RecordType> myNewMap = new Map<String,RecordType>();

for (string id: acctRecordTypes.keySet()){

myNewMap.put(acctRecordTypes.get(id).Name, acctRecordTypes.get(id));

}

 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

Jake GmerekJake Gmerek

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.

GravityGravity

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"
  },

 

 

Jake GmerekJake Gmerek

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:

 

 

Map<String,RecordType> acctRecordTypes = new Map<String,RecordType>([select NAME from RecordType where SOBJECTTYPE='Account']);

Map<String,RecordType> myNewMap = new Map<String,RecordType>();

for (string id: acctRecordTypes.keySet()){

myNewMap.put(acctRecordTypes.get(id).Name, acctRecordTypes.get(id));

}

 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.

This was selected as the best answer
GravityGravity

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.