You need to sign in to do that
Don't have an account?

de-reference a null object (help)
I keep getting a null object error on this part of my controller but can't figure out why...
I am trying to determine the age of the opportunity as CloseDate - CreateDate for closed opportunities and Today - Created Date for open opportunities. I can't quite figure out how any of these could be null... and in anycase I tried to account for that in the code but is still haunting me...
public integer getAge() { date close; if( opp.CloseDate == null) {close = system.today();} else {close = opp.CloseDate;} integer openAge = date.valueof(opp.CreatedDate).daysbetween(opp.CloseDate); integer closeAge = date.valueof(opp.CreatedDate).daysBetween(system.today()); if(opp.isClosed == true && closeAge != null && openAge != null) { return closeAge; } else {return openAge;} }
Any pointers are greatly appreciated!
Following works for me...when page is called with opportunity button..check if this helps you
No change in yr get age code.
public class OpportunityExt {
private Opportunity opp;
public OpportunityExt(ApexPages.StandardController stdController) {
this.opp = (opportunity)stdController.getRecord();
this.opp = [select Id,CloseDate,CreatedDate,IsClosed from Opportunity where Id = :this.opp.Id];
}
public integer getAge() {
date close;
if( opp.CloseDate == null) {close = system.today();} else {close = opp.CloseDate;}
date cdate = date.valueof(opp.CreatedDate);
integer openAge = cdate.daysbetween(system.today());
integer closeAge = cdate.daysBetween(close);
if(opp.isClosed == true && closeAge != null && openAge != null) { return closeAge + 1; } else {return openAge + 1;}
}
}
<apex:page standardController="Opportunity" extensions="OpportunityExt">
{!age}
</apex:page>
All Answers
Is opp in scope and has a value?
Yes, here is the full controller:
and here is the testclass
StdController will just return you Opp Id. You need to query opportunity object using that Id.
try it....
That didnt fix the issue. Plus, the other functions work properly without explicitly calling out those fields in a query.
I think it has something to do with the daysBetween function...
Following works for me...when page is called with opportunity button..check if this helps you
No change in yr get age code.
public class OpportunityExt {
private Opportunity opp;
public OpportunityExt(ApexPages.StandardController stdController) {
this.opp = (opportunity)stdController.getRecord();
this.opp = [select Id,CloseDate,CreatedDate,IsClosed from Opportunity where Id = :this.opp.Id];
}
public integer getAge() {
date close;
if( opp.CloseDate == null) {close = system.today();} else {close = opp.CloseDate;}
date cdate = date.valueof(opp.CreatedDate);
integer openAge = cdate.daysbetween(system.today());
integer closeAge = cdate.daysBetween(close);
if(opp.isClosed == true && closeAge != null && openAge != null) { return closeAge + 1; } else {return openAge + 1;}
}
}
<apex:page standardController="Opportunity" extensions="OpportunityExt">
{!age}
</apex:page>
Thanks! That worked!
I missed with switch from public final to public, but it works now!!
Thanks again!