+ Start a Discussion
SandrineSandrine 

null pointer exception for a map

Hello,

I am getting a null pointer exception in below code.
how can i put a check on below code to check if it is null or not
public static Map<String, Map<Id, RecordType>> rtTemp = new Map<String, Map<Id, RecordType>>(), 
public SObjectType oName;
public static final String RT_ID = 'rtTemp';

public void setSObjectType(SObjectType objT) {
   this.oName = objT;
}

function()
{      
	Map<Id, RecordType> rtMap = rtTemp.get(String.valueOf(Account.SObjectType));

	String rtTemp = (String) obj.get(RT_ID);
	
	String rtDName = rtMap.get(rtTemp).DeveloperName;	 // Null pointer exception
}
thank you for suggestion
 
Best Answer chosen by Sandrine
ShirishaShirisha (Salesforce Developers) 
Hi Sandrine,

Greetings!

You can either check for the size of the variable or list which you suspect is returning as null before executing the code or use exception handling as suggested here.

https://help.salesforce.com/articleView?id=000327918&type=1&mode=1

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Sandrine,

Greetings!

You can either check for the size of the variable or list which you suspect is returning as null before executing the code or use exception handling as suggested here.

https://help.salesforce.com/articleView?id=000327918&type=1&mode=1

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
AnudeepAnudeep (Salesforce Developers)