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
Rolando EstevesRolando Esteves 

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

Hi,

 

Thanks for the help in advance!

 

Im currently working on a trigger that is supposed to insert ids of the quotes into a detail custom object named CSV_UPLOAD_HISTORY on the field (Quote__c Lookup(Quote)). Im getting an error that says im attempting to dereference a null object do not understand why (System.NullPointerException: Attempt to de-reference a null object: Trigger.assignQuoteIds: line 27, column 1).

 

Here is my code:

 

trigger assignQuoteIds on Quote (before insert) {
    
// Create the list that will contain all quote ID's
    List<Id> l_quote = new List<Id>();
    for(Quote qt: Trigger.new)
    {
        //Collect all Quote ID's
       l_quote.add(qt.OpportunityID);
    }
    
    // Map in order to match Quotes with their respectives CUH's
    Map<ID, CSV_UPLOAD_HISTORY__c> qtMap = new Map<Id, CSV_UPLOAD_HISTORY__c>();
    
    // Loop that creates the correct map between OppIDS and CUH's
    for(CSV_UPLOAD_HISTORY__c cuh : [SELECT OpportunityID__c
    FROM CSV_UPLOAD_HISTORY__c WHERE OpportunityID__c IN :l_quote])
    {
        qtMap.put(cuh.OpportunityID__c, cuh);  // Creating a map with the CUH's
    }
    
    // List in order to update the records with
    List<CSV_UPLOAD_HISTORY__c> l_cuh = new List<CSV_UPLOAD_HISTORY__c>();
    for(Quote qt: Trigger.new)
    {
        CSV_UPLOAD_HISTORY__c cuh = new CSV_UPLOAD_HISTORY__c ();
        cuh = qtMap.get(qt.OpportunityID);
        cuh.Quote__c = qt.Id; // line 27 !!!
        l_cuh.add(cuh);
    }
    
    //update l_cuh;
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kerwintangkerwintang

This means that cuh is null on line 27.

 

That would happen if there are no CSV_UPLOAD_HISTORY records for that Opportunity ID (in which case there will be no entry in qtMap for that OpportunityID, leading to the null cuh).

 

Here's my suggested solution:

 

    for(Quote qt: Trigger.new)
    {
        CSV_UPLOAD_HISTORY__c cuh = qtMap.get(qt.OpportunityID);
        if(cuh!=null){
          cuh.Quote__c = qt.Id; // line 27 !!!
          l_cuh.add(cuh);
}
 }

 Hope this helps. cheers!

All Answers

vriavmvriavm

Hi,

 

    Is the OpportunityId populated in the Quote. Can you please check this?

 

 

Rolando EstevesRolando Esteves

Hi thanks for the early replay. It is a before insert trigger so im unable to create quotes since i get the error message. But the error i get is on the line when I try to insert the quote ID into the lookup field on my custom object.

Rolando EstevesRolando Esteves

A friend gave me some advice about changing the trigger to after update but im still recieving the same error.

vriavmvriavm

How are you creating a Quote?

Is ti from Opportunity screen?

kerwintangkerwintang

This means that cuh is null on line 27.

 

That would happen if there are no CSV_UPLOAD_HISTORY records for that Opportunity ID (in which case there will be no entry in qtMap for that OpportunityID, leading to the null cuh).

 

Here's my suggested solution:

 

    for(Quote qt: Trigger.new)
    {
        CSV_UPLOAD_HISTORY__c cuh = qtMap.get(qt.OpportunityID);
        if(cuh!=null){
          cuh.Quote__c = qt.Id; // line 27 !!!
          l_cuh.add(cuh);
}
 }

 Hope this helps. cheers!

This was selected as the best answer
Rolando EstevesRolando Esteves

The situation: I was inseting the id of the opportunity with this line of code :

 

 String oppID = System.currentPageReference().getParameters().get('id');

 So the id i was inserting was of 18 characters and not 21 like in the DB.

 

Thanks for everyones reply!