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
Pratap adminPratap admin 

Accesing MAP Value

Hi,

I am new to salesforce. while trying to access my map value, i am getting the exception like

System.NullPointerException: Attempt to de-reference a null object

My  declaration is like
map<ID,Customobj> mymap =new map<ID,Customobj> ();
ID is a custom object ID ( which is a lookup field in Customobj)
after putting the value, i can see the map size is 1

Customobj  obj = new Customobj();

i am trying to access the map value by providing the ID
obj =mymap.get(ID);  ==>verified ID is valid one

now trying to access the field of this object
obj.fieldname ====> getting Null pointer exception


Please advice
David BermanDavid Berman
Can you post the code on how you are populating your map?
AshlekhAshlekh
Hi,

There is simple step to avodi this type of error
 
//Check map is null or not null and contains the id 
if(mymap != null && mymap.containsKey(ID))
{
  obj =mymap.get(ID);  ==>verified ID is valid one
   if(obj !=null)
  {
    obj.fieldname ====> getting Null pointer exception
  }
}

Please mark it as a solution if it help you.