function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KitagawaSan85KitagawaSan85 

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!

 

Best Answer chosen by Admin (Salesforce Developers) 
varun.sforcevarun.sforce

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

crop1645crop1645

Is opp in scope and has a value? 

KitagawaSan85KitagawaSan85

Yes, here is the full controller: 

 

public class OpportunityExt {

    private final Opportunity opp;
    
    public OpportunityExt(ApexPages.StandardController stdController) {
        this.opp = (opportunity)stdController.getRecord();
    }
    

    public integer getOpenTasks() {
        return [SELECT count() FROM task WHERE whatid = :opp.id and IsClosed=false];
    } 

   
    public decimal getFunnel() {
        AggregateResult ar = [SELECT Sum(Lic_Revenue__c)s FROM opportunity WHERE ownerid = :opp.ownerid and Close_FQ__c = :opp.Close_FQ__c and Probability != 0];
        decimal RepFun; 
        	if ( ar.get('s') == null) {RepFun = 0;} else {RepFun = (decimal)ar.get('s');}
        	
        decimal d1;
        	if(opp.Lic_Revenue__c != null && RepFun != 0){d1 = (opp.Lic_Revenue__c / RepFun) * 100;} else {d1 = 0;}
        
        return d1.setScale(1);
    }
    
    public decimal getProposalScore() {
        AggregateResult ar2 = [SELECT AVG(total_score__c)a FROM Proposal__c WHERE Opportunity__c = :opp.id];
        decimal p1 = (decimal)ar2.get('a');
        return p1;
    }
     
    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;} 
    }   
    

and here is the testclass

 

public class OpportunityExt_TestClass {
static testMethod void OpportunityExt()
{
//Test converage for the myPage visualforce page
PageReference pageRef = Page.OpportunityQuickView;
Test.setCurrentPageReference(pageRef);

//Create Account
Account ac = new Account (name='XYZ Organization');
insert ac;


//Create Opportunity
Opportunity op = new Opportunity (name='test opportunity');
op.Account = ac;
op.Type = 'New Users';
op.Lic_Revenue__c = 5; 
op.StageName = 'Win'; 
op.Push_Count_FQ__c = 2; 
op.CloseDate = system.today()+365;
insert op;

//Create Opportunity
Opportunity op2 = new Opportunity (name='test opportunity');
op2.Account = ac;
op2.Type = 'New Users';
op2.Lic_Revenue__c = null; 
op2.StageName = 'Win'; 
op2.Push_Count_FQ__c = 2; 
op2.CloseDate = system.today()+365;
insert op2;

ApexPages.StandardController sc = new ApexPages.standardController(op);
// create an instance of the controller
OpportunityExt myPageOpp = new OpportunityExt(sc);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
integer OpenTasks = myPageOpp.getOpenTasks();
decimal Funnel = myPageOpp.getFunnel();
decimal ProposalScore = myPageOpp.getProposalScore();
string ShortID = myPageOpp.getShortID();
decimal AgeProb = myPageOpp.getAgeProb();
integer age = myPageOpp.getAge();


ApexPages.StandardController sc2 = new ApexPages.standardController(op2);
// create an instance of the controller
OpportunityExt myPageOpp2 = new OpportunityExt(sc2);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
integer OpenTasks2 = myPageOpp2.getOpenTasks();
decimal Funnel2 = myPageOpp2.getFunnel();
decimal ProposalScore2 = myPageOpp2.getProposalScore();
string ShortID2 = myPageOpp2.getShortID();
decimal AgeProb2 = myPageOpp2.getAgeProb();
integer age2 = myPageOpp.getAge();
}
}

 

 

varun.sforcevarun.sforce

StdController will just return you Opp Id. You need to query opportunity object using that Id.

 

try it....

 

this.opp = (opportunity)stdController.getRecord();
this.opp = [select Id,[Needed fields list...] from Opportunity where Id = :this.opp.Id];
KitagawaSan85KitagawaSan85

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... 

varun.sforcevarun.sforce

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>

This was selected as the best answer
N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar
 private final Opportunity opp = new Opportinity();


Try this

If it works mark it as solution
KitagawaSan85KitagawaSan85

Thanks! That worked! 

 

I missed with switch from public final to public, but it works now!!

 

Thanks again!