0

I am facing issue "Null pointer exception Attempt to deference a null object" while loading data from data loader. But when creating individual record it is not throwing any Error.

Here is my code :

 Map<String, Account> acmap = new Map<String,Account>();
 Set<ID> accIds = new Set<Id>();

List<customObject> cblist =  new List<customObject>();
    for(Account acc : [Select ID,Name,Number__C,example__r.ID from Account Where Number__C IN: numSet]){

        acmap.put(acc.Number__C, acc );   
        accIds.add(acc.example__r.ID); 

    }
 Map<id,Contact> mapcontact = new map<id,Contact>();
    for(Contact con : [Select ID,Name,AccountId  from Contact where AccountId IN: accIds])
    {
        mapContact.put(con.AccountId ,con);
    }

    for(CustomObject cb : Trigger.New){


            if(acmap.containskey(cb.Number__c)){
                cb.LookupAcc__c = acmap.get(cb.Number__c).example__r.ID;           


            }

            // While inserting the contact I am getting that error
              if(mapContact.containskey(acmap.get(cb.Number__c).example__r.ID)){
                     cb.LookupContact__c  = mapContact.get(acmap.get(kyc.Customer_Number__c).example__r.Id).ID;
                    } 
        cblist.add(cb);

    }
    insert cblist;
1
  • acmap may not contain that number; you should not assume that it does. Also, example__r.Id is the same as example__c, which would be the preferred way of accessing the Id from a performance perspective. Also, I'm pretty sure you're doing something wrong if you're inserting records in Trigger.new. You should be getting exceptions all over the place. – sfdcfox Feb 4 '16 at 22:36
0

Try this code.

Map<String, Account> acmap = new Map<String,Account>();
Set<ID> accIds = new Set<Id>();

List<customObject> cblist =  new List<customObject>();
    for(Account acc : [Select ID,Name,Number__C,example__c from Account Where Number__C IN: numSet]){

        acmap.put(acc.Number__C, acc );   
        accIds.add(acc.example__c); 

    }
 Map<id,Contact> mapcontact = new map<id,Contact>();
    for(Contact con : [Select ID,Name,AccountId  from Contact where AccountId IN: accIds])
    {
        mapContact.put(con.AccountId ,con);
    }

    for(CustomObject cb : Trigger.New){


        if(acmap.containskey(cb.Number__c)){
            cb.LookupAcc__c = acmap.get(cb.Number__c).example__c;           

            // While inserting the contact I am getting that error
            if(mapContact.containskey(cb.LookupAcc__c){
               cb.LookupContact__c  = mapContact.get(cb.LookupAcc__c).ID;
            } 
        }
        //If this is Insert event, you dont have to add them to a list 
        //cblist.add(cb);

    }
    //
    //insert cblist;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.