+ Start a Discussion
phil.adams1.3928016499835732E12phil.adams1.3928016499835732E12 

Attempt to de-reference null object with inline visualforce chart (on Account page)

For the life of me I can't seem to get this working. I've written a custom extension class and a basic visualforce page to display a chart. As a normal controller with a hardcoded Account Id it works fine and the chart displays. When I try and convert it into a controller extension and add it to an account page I keep getting 'Content cannot be displayed: Attempt to de-reference a null object' errors.

Visualforce Page
<apex:page standardController="Account" extensions="BudgetController2"> 
  <apex:pageBlock title="Budget Test"> 
 
    <apex:chart height="400" width="500" data="{!ChartData}">
          <apex:legend position="right"/>
          <apex:axis type="Numeric" position="left" fields="data1,data2"
                 title="Value" grid="true"/>
          <apex:axis type="Category" position="bottom" fields="name"
                 title="Month"/>
          <apex:barSeries orientation="vertical" axis="left"
                 xField="name" yField="data1" title="Budget"/>
          <apex:lineSeries axis="left" xField="name" yField="data2"
                 markerType="circle" markerSize="4" markerFill="#8E35EF" title="Targett"/>
    </apex:chart>

  </apex:pageBlock> 
</apex:page>

Custom Controller Extension
public with sharing class BudgetController2 {

    private Account acct;

    public BudgetController2(ApexPages.StandardController controller) {

        acct = (Account)controller.getRecord();

    }

    public list<AggregateResult> lstBud = [SELECT SUM(Jan__c)Jan, SUM(Feb__c)Feb, SUM(Mar__c)Mar FROM Account WHERE Id = :acct.id];
    public list<AggregateResult> lstTar = [SELECT SUM(Jan_target__c)Jan, SUM(Feb_target__c)Feb, SUM(Mar_target__c)Mar FROM Account WHERE Id = :acct.id];
    public list<list<AggregateResult>> lstAll = new list<list<AggregateResult>>{lstBud, lstTar};
   
    public List<Data> getChartData() {
        List<Data> data = new List<Data>();
        data.add(new Data('Jan', (decimal)lstAll[0][0].get('Jan'), (decimal)lstAll[1][0].get('Jan')));
        data.add(new Data('Feb', (decimal)lstAll[0][0].get('Feb'), (decimal)lstAll[1][0].get('Feb')));
        data.add(new Data('Mar', (decimal)lstAll[0][0].get('Mar'), (decimal)lstAll[1][0].get('Mar')));
        system.debug(data);
        return data;
    }   

    public class Data {
        public String name { get; set; }
        public Decimal data1 { get; set; }
        public Decimal data2 { get; set; }
        public Data(String name, Decimal data1, Decimal data2) {
            this.name = name;
            this.data1 = data1;
            this.data2 = data2;

        }
    }
   

}


The debug logs show that the code stops as soo as it trys to run the constructor. I've tried pretty much every code combination I can find online, although i'm pretty sure it will be a glaringly obvious error!

Any help appreciated.


Best Answer chosen by phil.adams1.3928016499835732E12
Sri549Sri549
Hello Phil,
I have gone through your complete code and its small mistake done.replace your class with this code given.

public with sharing class BudgetController2 {
public list<AggregateResult> lstBud{get;set;} 
public list<AggregateResult> lstTar{get;set;}
public list<list<AggregateResult>> lstAll{get;set;}
   
    private Account acct;

    public BudgetController2(ApexPages.StandardController controller) {

        acct = (Account)controller.getRecord();   
    lstBud = [SELECT SUM(Jan__c)Jan, SUM(Feb__c)Feb, SUM(Mar__c)Mar FROM Account WHERE Id = :acct.id];
    lstTar = [SELECT SUM(Jan_target__c)Jan, SUM(Feb_target__c)Feb, SUM(Mar_target__c)Mar FROM Account WHERE Id = :acct.id];
    lstAll = new list<list<AggregateResult>>{lstBud, lstTar};
   }
    public List<Data> getChartData() {
        List<Data> data = new List<Data>();
        data.add(new Data('Jan', (decimal)lstAll[0][0].get('Jan'), (decimal)lstAll[1][0].get('Jan')));
        data.add(new Data('Feb', (decimal)lstAll[0][0].get('Feb'), (decimal)lstAll[1][0].get('Feb')));
        data.add(new Data('Mar', (decimal)lstAll[0][0].get('Mar'), (decimal)lstAll[1][0].get('Mar')));
        system.debug(data);
        return data;
    }  

    public class Data {
        public String name { get; set; }
        public Decimal data1 { get; set; }
        public Decimal data2 { get; set; }
        public Data(String name, Decimal data1, Decimal data2) {
            this.name = name;
            this.data1 = data1;
            this.data2 = data2;

        }
    }
}

If it is not solving your problem please reply me so that i may help you with pleasure..

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer

All Answers

Sri549Sri549
Hello Phil,
I have gone through your complete code and its small mistake done.replace your class with this code given.

public with sharing class BudgetController2 {
public list<AggregateResult> lstBud{get;set;} 
public list<AggregateResult> lstTar{get;set;}
public list<list<AggregateResult>> lstAll{get;set;}
   
    private Account acct;

    public BudgetController2(ApexPages.StandardController controller) {

        acct = (Account)controller.getRecord();   
    lstBud = [SELECT SUM(Jan__c)Jan, SUM(Feb__c)Feb, SUM(Mar__c)Mar FROM Account WHERE Id = :acct.id];
    lstTar = [SELECT SUM(Jan_target__c)Jan, SUM(Feb_target__c)Feb, SUM(Mar_target__c)Mar FROM Account WHERE Id = :acct.id];
    lstAll = new list<list<AggregateResult>>{lstBud, lstTar};
   }
    public List<Data> getChartData() {
        List<Data> data = new List<Data>();
        data.add(new Data('Jan', (decimal)lstAll[0][0].get('Jan'), (decimal)lstAll[1][0].get('Jan')));
        data.add(new Data('Feb', (decimal)lstAll[0][0].get('Feb'), (decimal)lstAll[1][0].get('Feb')));
        data.add(new Data('Mar', (decimal)lstAll[0][0].get('Mar'), (decimal)lstAll[1][0].get('Mar')));
        system.debug(data);
        return data;
    }  

    public class Data {
        public String name { get; set; }
        public Decimal data1 { get; set; }
        public Decimal data2 { get; set; }
        public Data(String name, Decimal data1, Decimal data2) {
            this.name = name;
            this.data1 = data1;
            this.data2 = data2;

        }
    }
}

If it is not solving your problem please reply me so that i may help you with pleasure..

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer
This was selected as the best answer
Offshore Freelance ConsultantOffshore Freelance Consultant
Hi,

below controller shoud work.

public with sharing class BudgetController2 {

    private Account acct;

    public BudgetController2(ApexPages.StandardController controller) {

        acct = (Account)controller.getRecord();

    }


  
    public List<Data> getChartData() {

    list<AggregateResult> lstBud = [SELECT SUM(Jan__c)Jan, SUM(Feb__c)Feb, SUM(Mar__c)Mar FROM Account WHERE Id = :acct.id];
    list<AggregateResult> lstTar = [SELECT SUM(Jan_target__c)Jan, SUM(Feb_target__c)Feb, SUM(Mar_target__c)Mar FROM Account WHERE Id = :acct.id];
    list<list<AggregateResult>> lstAll = new list<list<AggregateResult>>{lstBud, lstTar};
       
        List<Data> data = new List<Data>();
        data.add(new Data('Jan', (decimal)lstAll[0][0].get('Jan'), (decimal)lstAll[1][0].get('Jan')));
        data.add(new Data('Feb', (decimal)lstAll[0][0].get('Feb'), (decimal)lstAll[1][0].get('Feb')));
        data.add(new Data('Mar', (decimal)lstAll[0][0].get('Mar'), (decimal)lstAll[1][0].get('Mar')));
        system.debug(data);
        return data;
    }  

    public class Data {
        public String name { get; set; }
        public Decimal data1 { get; set; }
        public Decimal data2 { get; set; }
        public Data(String name, Decimal data1, Decimal data2) {
            this.name = name;
            this.data1 = data1;
            this.data2 = data2;

        }
    }
  

}



----------
Hope this helps!

Regards

*******************************

J G
phil.adams1.3928016499835732E12phil.adams1.3928016499835732E12
Thanks, I tried Srinivas suggestion and that worked although both would have solved my problem by the looks of it. Thanks to both of you, saved me a lot of time!