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
JIM LJIM L 

Problem with Map's

Hi All,

Im trying to get the account name from a map in the example below, but get a null pointer excpetion, any idea why?

public class MainClass {
map<id, account> amap = new map<id, account>([select id, name, createdbyid from account]);
map<id,user> umap = new map<id, user>([select id, name from user]);
public void met1(){
        map<id, opportunity> omap = new map<id, opportunity>([select id, name, createdbyid, accountid from opportunity]);
         for(opportunity o : omap.values()){
                system.debug(omap.get(o.id).name); //This works find and give me the opp name
                system.debug(amap.get(o.accountid).name);   // this one dosent ??
            
         }   
    }   
}
Best Answer chosen by JIM L
Suraj TripathiSuraj Tripathi
Hi  JIM L,
Plesae Try this piece of code .
public class MainClass {
//map<id,user> umap = new map<id, user>([select id, name from user]);
public static void met1(){
    map<id, account> amap = new map<id, account>([select id, name, createdbyid from account]);
        map<id, opportunity> omap = new map<id, opportunity>([select id, name, createdbyid, accountid from opportunity]);
         for(opportunity o : omap.values()){
                 system.debug(omap.get(o.id).name); //This works find and give me the opp name
             if(amap.containsKey(o.AccountId)){
                  system.debug(amap.get(o.accountid).name);  
             }   
         }
  
    }   
}
Hope it's help you.

Regard Suraj