3

I've got some simple code and I'm wondering if someone can help me understand why it would result in the "Attempt to de-reference a null object" error? This is part of a custom controller that I'm using for a Visualforce page. Please let me know if I can provide any additional info for clarity. Thank you!

public with sharing class NewBusinessForecastingController {

    private ApexPages.StandardController controller {get; set;}
    public List<SBQQ__Quote__c> q                   {get; set;}
    public List<SBQQ__Quote__c> clonedQuoteList     {get; set;}

    public NewBusinessForecastingController(ApexPages.standardController std) {

        q = [ 
            SELECT 
            SBQQ__Opportunity2__r.Quarter_View__c, 
            SBQQ__Opportunity2__r.Account.Name,
            SBQQ__Opportunity2__r.CloseDate,
            SBQQ__Opportunity2__r.Name,             
            SBQQ__Opportunity2__r.Id, 
            Quote_Name__c,
            Name,
            Id, 
            (SELECT 
                SBQQ__SubscriptionTerm__c,
                Delivery_Start_Date__c,
                Delivery_Frequency__c,
                SBQQ__Product__r.Name,
                Delivery_End_Date__c,
                Line_Description__c,  
                Data_Start_Date__c, 
                Data_End_Date__c,  
                Name,
                Id
                FROM SBQQ__LineItems__r)
                FROM SBQQ__Quote__c 
                    WHERE SBQQ__Opportunity2__r.OwnerId = :UserInfo.getUserId() AND
                      SBQQ__Opportunity2__r.Probability > 0 AND
                      SBQQ__Opportunity2__r.Probability < 96 AND
                      SBQQ__Primary__c = TRUE AND 
                      Id IN (SELECT SBQQ__Quote__c FROM SBQQ__QuoteLine__c)
                      ORDER BY SBQQ__Opportunity2__r.CloseDate
        ];

        for(SBQQ__Quote__c cloneQuote : q) {
            SBQQ__Quote__c clonedQuote = cloneQuote.clone();
            clonedQuoteList.add(clonedQuote);
        }

    }

If I comment out the line that adds each record to the list, I don't encounter the error. The line below is line 41, which is where the fatal error is encountered.

clonedQuoteList.add(clonedQuote);
0

2 Answers 2

6

You never instantiate your list. When you declare a variable, it is null until some value is assigned.

List<Object> someVariable;
system.assertEquals(null, someVariable, 'This variable has not yet been assigned');

So make sure you assign a new List before trying to add to your collection variable:

clonedQuoteList = new List<SBQQ__Quote__c>();
for (SBQQ__Quote__c quote : q)
{
    clonedQuoteList.add(quote.clone());
}

Or, you can just clone the list wholesale and skip the for loop:

clonedQuoteList = q.deepClone();
0
3

You never initialize the clonedQuoteList property, so on line 41 you're trying to call the equivalent of null.add().

You need this line someplace prior to your call to add items to the list.

clonedQuoteList = new List<SBQQ__Quote__c>();

1
  • Thanks for your help! I've initialized now and got it to work. Really appreciate it. Oct 19, 2017 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .