+ Start a Discussion
Web-passWeb-pass 

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

Hello,

 

I am relatively new to Apex and Visualforce and need some guidance.

 

I have 2 objects: Billing Account and Line Items. For every Account, there can be multiple Billing Accounts each of which can have multiple Line Items. I want to return a List from a function that will give me all the Line Items of all Billing Accounts in one single list. This code compiles but on running the code, it gives me an error

 

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

totallines.addAll(lines);
Please help my cause by suggesting what may be causing this problem. Thanks !!

 

Here is my apex class snippet: 

 

List<Line_Items__c> l, lines, totallines;
List<Billing_Account__c> billacc;   
public List<Line_Items__c> getLineItems()
    {
        if(invoice.Billing_Account__c != null) 
        {
            l = [select id, name, Plan__c, Rate__c, Quantity__c, Total_Value__c, Activation_Date__c, Plan_Name__c, Street__c,
Unit__c, City__c from Line_Items__c where Service_Account__c =: invoice.Billing_Account__c and Status__c =: 'Active'];
           
return l;
                       
        }
        else
        { 
            billacc = [select id, Bill_Street__c, Bill_Unit_Number__c, Bill_City__c, Bill_State__c, Bill_Postal_Code__c from Billing_Account__c 
                                    where Account__c =: invoice.Account__c];
            
            integer z = 0;  
            if(billacc.size() > 0)
            {                      
                for(integer k =0; k < billacc.size(); k++)
                {
                   lines = [select id, name, Plan__c, Rate__c, Quantity__c, Total_Value__c, Activation_Date__c, Plan_Name__c,
Street__c, Unit__c, City__c from Line_Items__c where Service_Account__c =: billacc[k].id and Status__c =: 'Active'];

                   for( integer h = 0; h < lines.size(); h++)
                   {
                       totallines.addAll(lines);
                       z++;
                   }

                }
                 
                 return totallines;
             }
             
             return null;
             
         } 
                                      
    }
Best Answer chosen by Admin (Salesforce Developers) 
jhenningjhenning

Web-pass:

 

Looks like you need to instantiate the totallines list collection. You need the following:

 

 

totallines = new List<Line_Items__c>();

 

 

All Answers

jhenningjhenning

Web-pass:

 

Looks like you need to instantiate the totallines list collection. You need the following:

 

 

totallines = new List<Line_Items__c>();

 

 

This was selected as the best answer
Web-passWeb-pass

Thank You so much .... that solved my problem

jhenningjhenning

awesome, just click the "accept as solution" button to easily share this with others. Thanks!