+ Start a Discussion
Balasubramani DhanapalBalasubramani Dhanapal 

Apex class

Hi all, 
          I am begginer in salesforce.I have added below code in apex class.

"Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Attempt to de-reference a null object: []"
This type   of error.Please help me anyone.
 Apex class:

Private void AddPSProducts(Map<Id,Opportunity> newOppMap){
        List<Opportunity> listOppsToAddProduct=new list<Opportunity>();
        List<id> listAccIds=new List<Id>();                
        List<id> listPSIds=new List<Id>();
        for(Opportunity opp:newOppMap.values()){        //Collecting the Accounts ids from the Triggered Opportunities
            if(opp.Project_Sheet__c!= NULL){
        //To avoid Unneccessary Executions.
        if(listPSIds.size() == 0) return;
        Map<Id,Account> customerMap=new Map<Id,Account>([select id,Delivery_Charged_for__c,Price_Book_ID__c from Account where id IN: listAccIds]);        
        Map<Id,Project_Sheet__c> psMap=new Map<Id,Project_Sheet__c>([SELECT id,Visit__r.Products__c,Site_Contract__c,Arrive__c,Depart__c,Visit__r.Quotation_Reference__c
                                                                     FROM Project_Sheet__c
                                                                     WHERE Id IN: listPSIds]);        
        for(Opportunity newOpp:newOppMap.values()){            
            if(psMap.containskey(newOpp.Project_Sheet__c) ){
                //Add Project Sheet Sheet Product to Opportunity
                addProduct=new OpportuntiyAutoProducts(psMap);
        }Catch(Exception e){
            String msg=e.getMessage();
            for(Opportunity o:newOppMap.values()){

Advance in Thanks
The error appears to be related to an insert operation, but I'm not seeing an insert operation in your code.  Also, these errors generally give you specifics about what is null, and it looks like it should be the validation field, but I don't see that in your code either.  Is there something missing, or am I reading it wrong?