Skip to main content
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?

 
3 answers
  1. Oct 14, 2015, 6:14 AM
    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.

     
0/9000