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
Mathew Andresen 5Mathew Andresen 5 

getter question

Hi,

Normally when for my getters I use the short hand.  For example
public Opportunity opp {get; set; }
And this works fine

However, I was trying that with a custom class I built and when I try and do it that way I get the error

"System.NullPointerException: Attempt to de-reference a null object 
Class.Top10OppAt90All_Class.<init>: line 27, column 1"
 
public class Top10OppAt90All_Class {
    
    public List<Opportunity> oppListStart {get; set;}
    OppWrapper newOpp;
    public List<OppWrapper> oppList {get; set;}   // = new List<oppWrapper>();
    public aggregateResult oppTotalClosed {get; set;}
    public decimal variance {get; set;}
    public Goals__c goal { get; set;}
    public Decimal oppTotal {get; set;}
    
    public Top10OppAt90All_Class() {
       // try {
            if (oppTotal == NULL) {oppTotal = 0;}
            if (oppListStart == NULL) { 
                oppListStart = [SELECT Name, StageName, Id, Amount, account.id, account.name, type, closedate FROM Opportunity 
                                                  WHERE StageName = 'A. Pending Sale' AND Amount != 0 AND Amount != NULL
                                                  ORDER BY Amount DESC LIMIT 10]; 
                
                oppTotalClosed = [SELECT Sum(Amount) TotalAmount FROM Opportunity WHERE StageName = 'W. Win' AND CloseDate = THIS_QUARTER];
                
            }
            
            for (Opportunity opp:oppListStart) {
				newopp = new OppWrapper(opp.name, opp.CloseDate, opp.Amount,  opp.id);
                system.debug('opp = ' + opp );
                system.debug('newopp = ' + newOpp);
                oppList.add(new OppWrapper(opp.name, opp.CloseDate, opp.Amount,  opp.id) );
                oppTotal = oppTotal + opp.amount;
            }
        oppList.add(new OppWrapper('Total', NULL, OppTotal, NULL));
      //  } catch (exception e) { system.debug(e); }    
        
        system.debug('oppList = ' + oppList);
    }
    
    /*
    public List<OppWrapper> getOppList() {
        return oppList;
    }  */
    
    public class OppWrapper {
        public String name {get; set; }
        public Date closeDate {get; set; }
        public Decimal amount {get; set; }
        public String Id {get; set; }
        
        public  OppWrapper(String name, Date closedate, Decimal amount, String id) {
            this.name = name;
            this.closeDate = closeDate;
            this.amount = amount;
            this.id = id;
            

            
        }
    }
    

}

The only way I've found to fix that is to do 
public List<OppWrapper> oppList  = new List<oppWrapper>();
combined with
/*
    public List<OppWrapper> getOppList() {
        return oppList;
    }  */

Why isn't the normal way working?





 
Best Answer chosen by Mathew Andresen 5
Tarun_KhandelwalTarun_Khandelwal
Hi Mathew,

You are getting this error because you are adding element in list before initializing the list - oppList. You can also use this way.
Declaire the list as you are doing -

public List<OppWrapper> oppList {get; set;} 

and before this for loop -

for (Opportunity opp:oppListStart) {
//
//
}

please initialize the list like -
oppList = new List<OppWrapper>();

It will also work fine. Let me know in case of any query.
 

All Answers

Tarun_KhandelwalTarun_Khandelwal
Hi Mathew,

You are getting this error because you are adding element in list before initializing the list - oppList. You can also use this way.
Declaire the list as you are doing -

public List<OppWrapper> oppList {get; set;} 

and before this for loop -

for (Opportunity opp:oppListStart) {
//
//
}

please initialize the list like -
oppList = new List<OppWrapper>();

It will also work fine. Let me know in case of any query.
 
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
Ok, I get that, but why don't have to do that when I do it with an opportunity?
Mathew Andresen 5Mathew Andresen 5
Ok, after some further testing I "think" the difference is that when I was doing it before I was initalizing the list by using the = sign.  Doing something like
oppList = [SELECT ...];

But trying to do 
oppList.add(...);

doesn't work the same, you have to initalize it first.