1

This basic controller below should work. I get the Content cannot be displayed: Attempt to de-reference a null object. Controller and Visualforce Issue error on the Visualforce page. Looks like my Spend__c is somehow empty.

Controller:

public class StatController {

public Date lastMonthEndDate{get;set;}
public String CompanyID{get;set;}

public StatController(ApexPages.standardController Controller){

getData(); }

public void getData(){

//ApexPages get Id appears to be the issue. 
//I have used the exact style shown below for several Visualforce charts with no issues. 

Spend__c calcRecord =[SELECT Id, MonthEnd__c, CompanyID__c, 
                           FROM Spend__c
                           WHERE Account__c = :ApexPages.currentPage().getParameters().get('id')
                         ORDER BY MonthEnd__c 
                         DESC 
                         LIMIT 1];

Date lastMonthEndDate = calcRecord.MonthEnd__c;
String CompanyID = calcRecord.CompanyID__c;

}

Visualforce:

<apex:page standardController="Account" extensions="StatController">

    <apex:outputText label="TEST: " value="{!lastMonthEndDate}"/>

</apex:page>

FIGURED IT OUT! I had code not shown here with "+=". That was causing the de-referencing issue. Took about 6 hours to figure it out :/

1

The actual problem is not the Spend__c calcRecord being empty(null). Its lastMonthEndDate being empty(null).

You have two variables named lastMonthEndDate one as member variable and another as local variable. When your constructor execute, it assigns value to the local variable. But when the visualforce page loads, it gets the value from the member variable but its null and throws the error.

Please remove the Date in this line

Date lastMonthEndDate = calcRecord.MonthEnd__c;

to

lastMonthEndDate = calcRecord.MonthEnd__c;

Hope it helps.

  • @Still the same issue, and error. Any thoughts? – Steel Reserve 211 May 23 '16 at 22:21
  • if I remove the set from public Date lastMonthEndDate{get;set;}. I get the error: Compile Error: member variable not visible for assignment at line 31 column 24 (the lastMonthEndDate = calcRecord.MonthEnd__c; line). I am confused. – Steel Reserve 211 May 23 '16 at 23:58
1

It is not clear to me what the problem is with you code, but this is how I would write it:

public class StatController {

    public Spend__c spend {get; set;}

    public StatController(ApexPages.standardController sc) {
        if (sc.getId() != null) {
            for (Spend__c s : [
                    SELECT Id, MonthEnd__c, CompanyID__c
                    FROM Spend__c
                    WHERE Account__c = :sc.getId()
                    ORDER BY MonthEnd__c DESC 
                    LIMIT 1
                    ]) {
                spend = s;
            }
        }
    }
}

<apex:page standardController="Account" extensions="StatController">
    <apex:pageMessages/>
    <apex:pageBlock>
        <apex:pageBlockSection rendered="{!spend != null}">
            <apex:outputField value="{!spend.MonthEnd__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

If you open the page passing the ID of a valid Account that has child Spend__c objects by adding this to the page URL ?id=xxxxxxxxxxxxxxx the page should display something.

  • I tried dropping in the account id into the query directly. Still no luck. Would void be causing the issue, since it cannot return anything? – Steel Reserve 211 May 23 '16 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.