+ Start a Discussion
Michael3.BrownMichael3.Brown 

I need Help Determining Why This Value is Null

Hello,

 

I have the following code which stores all of my my records for a given object into a custom list: totalRecords. I have a VF page that simply displays each record in this list.

 

public class ListView 
{

List<Pipeline_Tracker_Ver_2__c> totalrecords = new List <Pipeline_Tracker_Ver_2__c>();
Integer total;

    public List<Pipeline_Tracker_Ver_2__c> getTotalRecords()
    {            
                
        for (Pipeline_Tracker_Ver_2__c tr : [SELECT         
                 Sales_Pole__c,  Probability__c, Q1_Revenue__c,
        FROM Pipeline_Tracker_Ver_2__c          
        WHERE Year__c = '2011'])
            {                       
                total = total + tr.Q1_Revenue__c; 
                totalrecords.add(tr);                     
            }    
        
        return totalrecords;
    }

    public Integer getgrandTotal()
    {
        return total;
    }
    
}

 

 

Whenever I take out the variable, total, my list populates fine on my VF page. However, my statement:

totalrecords.add(tr); 

 seems to be causing problems. When I save my Apex Class, there are no errors, but when I try and load my VG page, I receive:

 

 

point System.NullPointerException: Attempt to de-reference a null object
Class.ListView.getTotalRecords: line 26, column 51 External entry point

 

 

Can anyone provide any insight into what I might be doing wrong. Am I calculating the total incorrectly since it seems to be finding a null value?

 

Thanks!

Mike

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Hmm.  Is Q1_Revenue__c guaranteed to be filled in?  I'm just wondering if adding null to an integer gives a problem.

 

Otherwise its difficult to see anything wrong - if total and tr are non-null I can't see how a null pointer exception could be thrown from the code.

All Answers

bob_buzzardbob_buzzard

The stacktrace doesn't quite seem to match up with the code:

 

The stack refers to: Class.ListView.getTotalRecordsDone

but the class has getTotalRecords

 

Also line 26 doesn't seem to quite match with the line of code.

Michael3.BrownMichael3.Brown

Hey Bob. Sorry about that. You can ignore the getTotalRecordsDone.   My actual list is called totalrecordsDone, and my original method was getTotalRecordsDone(), but when posting on here I tried to remove the Done to prevent confusing everyone, to try and keep my naming as straighforward as possible.. Also, I have additional code, but I didn't want to paste it on here because I wanted to try and keep it simple.  But, in my original code, line 26 refers to:

 

total = total + tr.Q1_Revenue__c;

 

Thanks,

Mike

bob_buzzardbob_buzzard

I'd say you need to initialise your total to zero, as primitives in Apex initialise to null.

 

You can do this at the top of the method:

 

 

 

 {            
        total=0;        
        for (Pipeline_Tracker_Ver_2__c tr : [SELECT         
                 Sales_Pole__c,  Probability__c, Q1_Revenue__c,
        FROM Pipeline_Tracker_Ver_2__c          
        WHERE Year__c = '2011'])
            {                       
                total = total + tr.Q1_Revenue__c; 
                totalrecords.add(tr);                     
            }    
        
        return totalrecords;
    }

 

 

Michael3.BrownMichael3.Brown

Thanks, Bob. Unfortunately, I'm still getting the same error after initializing total to equal 0.

 

Do you think there is something wrong with the way I am trying to add the field, Q1_Revenue__c. This is the field I need to find the grand total of, but I wasn't sure if tr.Q1_Revenue__c was the proper way to call it.

 

Thanks,

Mike

bob_buzzardbob_buzzard

Hmm.  Is Q1_Revenue__c guaranteed to be filled in?  I'm just wondering if adding null to an integer gives a problem.

 

Otherwise its difficult to see anything wrong - if total and tr are non-null I can't see how a null pointer exception could be thrown from the code.

This was selected as the best answer
Michael3.BrownMichael3.Brown

That's a great point. My revenue field isn't required to be filled in, so it may be trying to store NULL as an integer. Let me throw in an IF loop to add 0 if the value of Revenue is Null at a given point.

Michael3.BrownMichael3.Brown

Thanks, for all your help, Bob. That was it. If I use an if statement to handle NULL values and add a 0 to my total, everything works properly.

 

- Mike