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
pavan kumar 1864pavan kumar 1864 

null pointer exception on class

public class triggerclassss {
    public set<id> aa= new set<id>();
    
    public  void accountss(list<contact>aaas){
        
        system.debug('contacts----'+aaas);
        
        for(contact c : aaas)
        {
            aa.add(c.accountid);
            
            system.debug('account ids----'+ c);
        }
        
        
        
        map<id,account> hg =  new map<id,account>([select id,name,phone from account where id in : aa]);
        
        system.debug('account with id and records'+hg);
        for(contact c : aaas)
        {
            
            if(c.accountid == hg.get(c.accountid).id)
            {
                
                system.debug('matching id with accuont and contact'+hg);
                c.phone = hg.get(c.accountid).phone;
            }
            system.debug('assigning contact phone'+c.phone);
        }
    }
}

can anyone please tell me why null pointer exception displaying at line 23
Best Answer chosen by pavan kumar 1864
mukesh guptamukesh gupta
Hi Pavan,

Please use belwo code:-
 
public class triggerclassss {
    public set<id> aa= new set<id>();
    
    public  void accountss(list<contact>aaas){
        
        system.debug('contacts----'+aaas);
        
        for(contact c : aaas)
        {
            aa.add(c.accountid);
            
            system.debug('account ids----'+ c);
        }
        
        
        
        map<id,account> hg =  new map<id,account>([select id,name,phone from account where id in : aa]);
        
		
        system.debug('account with id and records'+hg);
		
		if(hg.size() > 0){
			for(contact c : aaas)
			{
			Id accRecordId = hg.get(c.accountid).id;
				if(accRecordId != null){
					if(c.accountid == hg.get(c.accountid).id)
					{
						
						system.debug('matching id with accuont and contact'+hg);
						c.phone = hg.get(c.accountid).phone;
					}
				}
				
				system.debug('assigning contact phone'+c.phone);
			}
		}
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Pavan,

Please use belwo code:-
 
public class triggerclassss {
    public set<id> aa= new set<id>();
    
    public  void accountss(list<contact>aaas){
        
        system.debug('contacts----'+aaas);
        
        for(contact c : aaas)
        {
            aa.add(c.accountid);
            
            system.debug('account ids----'+ c);
        }
        
        
        
        map<id,account> hg =  new map<id,account>([select id,name,phone from account where id in : aa]);
        
		
        system.debug('account with id and records'+hg);
		
		if(hg.size() > 0){
			for(contact c : aaas)
			{
			Id accRecordId = hg.get(c.accountid).id;
				if(accRecordId != null){
					if(c.accountid == hg.get(c.accountid).id)
					{
						
						system.debug('matching id with accuont and contact'+hg);
						c.phone = hg.get(c.accountid).phone;
					}
				}
				
				system.debug('assigning contact phone'+c.phone);
			}
		}
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Maharajan CMaharajan C
Hi Pavan,

Please use the contains key check:
 
public class triggerclassss {
    public set<id> aa= new set<id>();
    
    public  void accountss(list<contact>aaas){
        
        system.debug('contacts----'+aaas);
        
        for(contact c : aaas)
        {
			if(c.accountid != null)
				aa.add(c.accountid);
            
            system.debug('account ids----'+ c);
        }
        
        
        
        map<id,account> hg =  new map<id,account>([select id,name,phone from account where id in : aa]);
        
        system.debug('account with id and records'+hg);
        for(contact c : aaas)
        {
            if(hg.containsKey(c.accountid)){
				if(hg.get(c.accountid).phone != null)
				{
					system.debug('matching id with accuont and contact'+hg);
					c.phone = hg.get(c.accountid).phone;
				}
			}
            system.debug('assigning contact phone'+c.phone);
        }
    }
}

Thanks,
Maharajan.C
Suraj Tripathi 47Suraj Tripathi 47
Hi Pavan,
Greetings!

In Map hg, When accountId is not found in the Map, then it will return NULL and you are trying to access the Id field from NULL.
So the error occurs, please check your accountId is present on the map.
Use this code to check it exists.
if(hg.containsKey(c.accountid))
If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
pavan kumar 1864pavan kumar 1864
Hi, @Mukesh Gupta,
How can I connect with you?
if I got stuck at any point in the future.
mukesh guptamukesh gupta
Hi Pawan,

you can drop email on :- mkrgupta1982@gmail.com