+ Start a Discussion
Douglas TrasterDouglas Traster 

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

I am getting the following error on a trigger I am trying to initiate.

System.NullPointerException: Attempt to de-reference a null object: Trigger.activeOpp: line 42, column 1

Help!!! I know it is a null value on one, but not sure where to change the code...

trigger activeOpp on Opportunity (after insert, after update) {
    
    List<contact> conUpdList = new List <contact>();
    List<Id> oppConIDList = new List<Id>();
    List<opportunity> oppConList = new List<opportunity>();
    List<Contact> conList = new List<Contact>();
    Map<ID, Contact> conMap = new Map<ID, Contact>();

    
    for (opportunity opp: Trigger.new){
        if(opp.Contact__c!= null){
            oppConList.add(opp);
            oppConIDList.add(opp.Contact__c);
        }
    }   
    
    if(!oppConIDList.isEmpty()){   
        conList  = [select Name__c, Active_Opportunity__c from contact where Name__c = :oppConIDList];
    }
        
    if(!conList.isEmpty()){
        for(Contact con : conList){
            conMap.put(con.ID,con);     
        }
    }
    
    if(!oppConList.isEmpty()){    
        for(Opportunity optyObj : oppConList){
            Contact c = conMap.get(optyObj.Contact__c);
            if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
                c.Active_Opportunity__c = False;
            }
            
            else if(optyObj.StageName == 'Prospecting'
            || optyObj.StageName == 'Qualified Interest'
            || optyObj.StageName == 'Needs Analysis'
            || optyObj.StageName == 'Value Proposition'
            || optyObj.StageName == 'Proposal/Price Quote'
            || optyObj.StageName == 'Negotiation/Review'
            || optyObj.StageName == 'Verbal Commit'
            || optyObj.StageName == 'Closed Pending'){
                c.Active_Opportunity__c = True;
            }
            conUpdList.add(c);        
        }
    }
    if(!conUpdList.isEmpty()){
        update conUpdList;
    }
}
Best Answer chosen by Douglas Traster
surasura
on the line   Contact c = conMap.get(optyObj.Contact__c);  you have to make sure map in not returning a null  modified code is given below 
trigger activeOpp on Opportunity (after insert, after update) {
    
    List<contact> conUpdList = new List <contact>();
    List<Id> oppConIDList = new List<Id>();
    List<opportunity> oppConList = new List<opportunity>();
    List<Contact> conList = new List<Contact>();
    Map<ID, Contact> conMap = new Map<ID, Contact>();

    
    for (opportunity opp: Trigger.new){
        if(opp.Contact__c!= null){
            oppConList.add(opp);
            oppConIDList.add(opp.Contact__c);
        }
    }   
    
    if(!oppConIDList.isEmpty()){   
        conList  = [select Name__c, Active_Opportunity__c from contact where Name__c = :oppConIDList];
    }
        
    if(!conList.isEmpty()){
        for(Contact con : conList){
            conMap.put(con.ID,con);     
        }
    }
    
    if(!oppConList.isEmpty()){    
        for(Opportunity optyObj : oppConList){
            Contact c = conMap.get(optyObj.Contact__c);
            if( c!= null )
            {
                if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
                    c.Active_Opportunity__c = False;
                }
            
                else if(optyObj.StageName == 'Prospecting'
                || optyObj.StageName == 'Qualified Interest'
                || optyObj.StageName == 'Needs Analysis'
                || optyObj.StageName == 'Value Proposition'
                || optyObj.StageName == 'Proposal/Price Quote'
                || optyObj.StageName == 'Negotiation/Review'
                || optyObj.StageName == 'Verbal Commit'
                || optyObj.StageName == 'Closed Pending'){
                    c.Active_Opportunity__c = True;
                }
                conUpdList.add(c);
            }        
        }
    }
    if(!conUpdList.isEmpty()){
        update conUpdList;
    }
}

 

All Answers

surasura
on the line   Contact c = conMap.get(optyObj.Contact__c);  you have to make sure map in not returning a null  modified code is given below 
trigger activeOpp on Opportunity (after insert, after update) {
    
    List<contact> conUpdList = new List <contact>();
    List<Id> oppConIDList = new List<Id>();
    List<opportunity> oppConList = new List<opportunity>();
    List<Contact> conList = new List<Contact>();
    Map<ID, Contact> conMap = new Map<ID, Contact>();

    
    for (opportunity opp: Trigger.new){
        if(opp.Contact__c!= null){
            oppConList.add(opp);
            oppConIDList.add(opp.Contact__c);
        }
    }   
    
    if(!oppConIDList.isEmpty()){   
        conList  = [select Name__c, Active_Opportunity__c from contact where Name__c = :oppConIDList];
    }
        
    if(!conList.isEmpty()){
        for(Contact con : conList){
            conMap.put(con.ID,con);     
        }
    }
    
    if(!oppConList.isEmpty()){    
        for(Opportunity optyObj : oppConList){
            Contact c = conMap.get(optyObj.Contact__c);
            if( c!= null )
            {
                if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
                    c.Active_Opportunity__c = False;
                }
            
                else if(optyObj.StageName == 'Prospecting'
                || optyObj.StageName == 'Qualified Interest'
                || optyObj.StageName == 'Needs Analysis'
                || optyObj.StageName == 'Value Proposition'
                || optyObj.StageName == 'Proposal/Price Quote'
                || optyObj.StageName == 'Negotiation/Review'
                || optyObj.StageName == 'Verbal Commit'
                || optyObj.StageName == 'Closed Pending'){
                    c.Active_Opportunity__c = True;
                }
                conUpdList.add(c);
            }        
        }
    }
    if(!conUpdList.isEmpty()){
        update conUpdList;
    }
}

 
This was selected as the best answer
Douglas TrasterDouglas Traster
THanks!  That gets rid of the error, but the check box, Active_opportunity__c is still not doing anything when I create an opportunity and fill in the value for Contact__c on it.  Is there something that I need to look at to get the field to update to check(true) or blank(false)??
surasura
Hi Dogulas,
no i dont think so 
since you have if and else if  not if and else block ther is possiblity your record may not enter neither if nor else if . If you put some system.debug statments from line 27 onwards and set a debug log you will be able to find exact execution path. 
hope this helps 
Douglas TrasterDouglas Traster
I keep getting errors and not able to see the system.debug(); from there?  I don't even see the trigger firing off to put anything in the logs.  I am beyond confused as what to do now.  The trigger doesn't work and I have to get this in and running.