+ Start a Discussion
CB312CB312 

Attempt to de-reference a null object

I'm stuck on this test class, I don't think I am consrtucting it correctly as I am incorrectly refrecing the object. This is my first time writing a test class for an extension class.

System.NullPointerException: Attempt to de-reference a null object - Class.testTotalGPbyDiv.testTotalGPbyDiv: line 4, column 1 External entry point
 

 

 

public class testTotalGPbyDiv {
static testMethod void testTotalGPbyDiv(){
Division__c div;
div.name = 'Hampton Test';
insert div;

ApexPages.currentPage().getParameters().put('id', div.Id);
ApexPages.StandardController std=new ApexPages.StandardController(div);

TotalGPbyDiv controller=new TotalGPbyDiv(std);

}
}

 

 


I'm really lost right now on how to get this to complie. Any thoughts?

 

_chris

Michael_JohnsonMichael_Johnson
public class testTotalGPbyDiv {
static testMethod void testTotalGPbyDiv(){
Division__c div = new Division__c();
div.name = 'Hampton Test';
insert div;

ApexPages.currentPage().getParameters().put('id', div.Id);
ApexPages.StandardController std=new ApexPages.StandardController(div);

TotalGPbyDiv controller=new TotalGPbyDiv(std);

}
}
CB312CB312

Thanks, knew I was missing something in the test class. However when I moved the code to production. It gives me that same error when rendereing the VF page. Content cannot be displayed: Attempt to de-reference a null object.

However nothing changed in my code from Sandbox to production.The code worked and generated the google viz object in sandbox, but now it throws that error in production.

I am so lost....argh,

VF page:

 

<apex:page standardController="Division__c" extensions="TotalGPbyDiv">
<html>
   <body>

      Number by day<br/>

    <c:ColumnChart jsondata="{!TotalGPbyDiv}" 
                Title="GP by Day" 
                is3d="True"
                focusBorderColor="Green"

    />

   </body>
</html>
</apex:page>

Controller:

 

public class TotalGPbyDiv {
        public TotalGPbyDiv(ApexPages.StandardController controller) {}
        //public list<AggregateResult> result = new list<AggregateResult>();
        public String getTotalGPbyDiv(){
        GoogleViz gv = new GoogleViz();
                     
            gv.cols = new list<GoogleViz.col> {
                new GoogleViz.Col('col1','Feature Date','date'),
                new GoogleViz.Col('col2','Total GP', 'number'),
                new GoogleViz.Col('col2','Total GMS', 'number')
            };
            ID DivId = ApexPages.currentPage().getParameters().get('id');
            Integer numOpportunities = 1;
            //string query = 'SELECT count(Feature_Date__c), sum(Total_GMS__c) FROM Opportunity WHERE Division_dynamic__c = :DivId';
            List <AggregateResult> result = [SELECT Feature_Date__c featuredate, sum(Total_GP__c) gp, sum(Total_GMS__c) gms FROM Opportunity WHERE Division_dynamic__c = :DivId GROUP BY Feature_Date__c ORDER BY Feature_Date__c ASC ];
            
            //for(Opportunity o : Database.query(query)){
            for(AggregateResult o : result){
        
                GoogleViz.row r = new GoogleViz.row();
                r.cells.add ( new GoogleViz.cell( (date)o.get('featuredate') ) );
                r.cells.add ( new GoogleViz.cell( (double)o.get('gp') ) );
                r.cells.add ( new GoogleViz.cell( (double)o.get('gms') ) );
                gv.addRow( r );
                numOpportunities++;
            }
     
            return gv.toJsonString();
} 
}

 Debug log:

 

15:27:43.129|SOQL_EXECUTE_BEGIN|[15]|Aggregations:0|SELECT Feature_Date__c featuredate, sum(Total_GP__c) gp, sum(Total_GMS__c) gms FROM Opportunity WHERE Division_dynamic__c = :DivId GROUP BY Feature_Date__c ORDER BY Feature_Date__c ASC 
15:27:43.197|SOQL_EXECUTE_END|[15]|Rows:101
15:27:43.197|CONSTRUCTOR_ENTRY|[20]|<init>()
15:27:43.197|CONSTRUCTOR_EXIT|[20]|<init>()
15:27:43.197|METHOD_ENTRY|[21]|LIST.add(ANY)
15:27:43.198|CONSTRUCTOR_ENTRY|[21]|<init>(Date)
15:27:43.198|METHOD_ENTRY|[21]|SObject.get(String)
15:27:43.198|METHOD_EXIT|[21]|SObject.get(String)
15:27:43.220|METHOD_ENTRY|[1]|01pC0000000TnNW|JSONObject.JSONObject()
15:27:43.220|METHOD_EXIT|[1]|JSONObject
15:27:43.220|CONSTRUCTOR_ENTRY|[151]|01pC0000000TnNW|<init>()
15:27:43.220|CONSTRUCTOR_EXIT|[151]|<init>()
15:27:43.220|METHOD_ENTRY|[168]|01pC0000000TnNW|JSONObject.putOpt(String, JSONObject.value)
15:27:43.221|CONSTRUCTOR_ENTRY|[168]|<init>(String)
15:27:43.221|METHOD_ENTRY|[168]|Date.format()
15:27:43.221|EXCEPTION_THROWN|[168]|System.NullPointerException: Attempt to de-reference a null object

 

I don't understand what happened between sandbox & production. Its the exact same code on the exact same fields. Grrrrrr

 

 

 

BritishBoyinDCBritishBoyinDC

Where are you creating the test opportunity record? Don't see it in the script below?

CB312CB312

I as having an issue with the Test class earlier. I've got that solved, and now I am trying to figure out why I get that error in production when the VF component loads. Its the exact same code from sandbox to production.

BritishBoyinDCBritishBoyinDC

When Prod fails but Sandbox works, it's usually because there was data in Sandbox making the test succeed that is missing in Live.

 

The error looks like it is falling over because a date field is null when it is not expecting it to be - that's why I would check that the Opportunity test record is being created correctly... 

Coco_SydneyCoco_Sydney

Agree.

 

If don't want to put extra code to catch null date, add value in Feature_Date__c