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
wajidawajida 

attempt to derefernce a null object error

Hi can anyone tell me why i am getting this error for this code? i am at my wits end. I have marked the line where i am getting the error

 

public class FindScope {
  list<list<opportunity>> theList;
  list<String> piclistVals = new list<string>();
  String s;
  list<string> pv;
  list<opportunity> listOfOpp; 
 
  public list<String> getPicklistVals(){
    Schema.DescribeFieldResult fieldResult = opportunity.stageName.getDescribe();
  for(Schema.PicklistEntry f :  fieldResult.getPicklistValues()){
    piclistVals.add(f.getValue());
   
  }
  return piclistVals;
  }
  public list<list<opportunity>> getOpportunities(){
         theList =  new list<list<opportunity>>();
         for(Integer i = 0; i < piclistVals.size(); i++){
         String pvalue = pv[i];  
       listOfOpp = [select id, name, stagename, amount from opportunity where stagename = :pvalue];
       thelist.add(listOfOpp);
         
   }
      return thelist;
  } 
   public list<opportunity> getProspecting(){
     
       oppProspecting = thelist[0];
        list<opportunity> myopp = new list<opportunity>();
         ;     
        return oppProspecting;
    }
     public list<opportunity> getQualification(){
      list<opportunity> oppQualification = new list<opportunity>();
       oppQualification = thelist[0];
        System.debug(oppQualification);       
        return oppQualification;
    }
      public list<opportunity> getNeedsAnalysis(){
      list<opportunity> oppNeedsAnalysis = new list<opportunity>();
       oppNeedsAnalysis = thelist[2];
        return oppNeedsAnalysis;
    } 
     public list<opportunity> getValueProposition(){
      list<opportunity> oppValueProposition = new list<opportunity>();
       oppValueProposition = thelist[3];
        return oppValueProposition;
    }       
    
}

chris.noechris.noe

Is there a reason why you declare a list and list "theList =  new list<list<opportunity>>();"?  Maybe try "theList =  new list<opportunity>();"

wajidawajida

thats because i need a list within a list

empucempuc

Hi wajida,

 

Few remarks to Your code:

 

1. invoking a SOQL queries in a loop is not a good idea.

2. I do not know what volume of data you are dealing with, but anyway, selecting all opportunities is rather a quite risky. Can't you filter and limit somehow the query? I do not know what exactly Your mechanism is designed to do but you should reconsider it.

3. Instead of building list of lists, maybe You could replace the loop of queries with single query and then iterate over the result and build a Map which for delivered opportunity status could return a List of found Opportunities?

4. The problem you are getting right now is quite obvious, variable thelist does not contain any elements. Its hard to answer why because I do not see the whole code and execution order... on the begining you can check whether the method getOpportunities was invoked before getProspecting...

 

 

davidjgriffdavidjgriff

You never defined what "oppProspecting" is.

What you want, I believe, is

 

list<opportunity> oppProspecting = thelist[0];

 

wajidawajida

Hi

 

Thanks for the insight. I have put a query inside the loop because I have very few opportunities and I am not worried about the governer limits. The question I want answered is I have put a list of opportunities by stagename in the list of lists in the getopportunities method and in the get prospecting method i am trying to access the first list of opportunites(ie that of prospecting). eventhough i have declared the variable listopp and the scope of the list of lists is the entire class i am still unable to get the first list in the getprospecting method. I find that you mentioned something called order of execution can you please elaborate on that? is it possible for my get prospecting method to execute before the get opportunities method?? if that is the case then it is quite obvious why I got the error. Please enlighten me on this and also let me know what the work around is

 

Thanks

Wajid

wajidawajida
david,
i added that also somehow i missed it when i copied the code...look below it is there for other methods even there i got the error
empucempuc

Hi Wajiida,

 

To make sure that the code execution does not cause the error add method getOpportunities invocation on Your controller. Then it will cause that variable will be initialized.

 

Or if You can please provide me a VF code. Then maybe I will be able to give You some additiona feedback.

 

 

Best regards!

wajidawajida

<apex:page controller="StageOpp" tabStyle="Account">
  <apex:form >
  <apex:pageBlock >
    <apex:pageBlockSection title="Prospecting">
  <apex:repeat value="{!prospecting}" var="p">
    {!p.name}{!p.amount}  <br/>
    </apex:repeat>
   </apex:pageBlockSection>   
    
    </apex:pageBlock>

  </apex:form>
</apex:page>

wajidawajida

public class StageOpp {
  public list<opportunity> listOfOpp;
  public string s;
  public list<string> picklistVals;
  public list<list<opportunity>> stagelist = new list<list<opportunity>>();   
public list<string> getPicklistVals() {
   picklistVals = new list<string>();
   Schema.DescribeFieldResult fieldResult = opportunity.stageName.getDescribe();
    for(Schema.PicklistEntry f  :  fieldResult.getPicklistValues()){
    picklistVals.add(f.getValue());
   }
    return picklistVals;     
  }

public list<list<opportunity>> getOpportunities(){
       list<string> pv = new list<string>();
       Schema.DescribeFieldResult fr = opportunity.stageName.getDescribe();
      
       for(Schema.PicklistEntry p  :  fr.getPicklistValues()){
        pv.add(p.getValue());
   }
         for(Integer i = 0; i < pv.size(); i++){
         String pvalue = pv[i];  
       listOfOpp = [select id, name, stagename, amount from opportunity where stagename = :pvalue];
       stagelist.add(listOfOpp);
         
   }
      return stagelist;
     }
     public list<opportunity> getProspecting(){
         list<string> pv = new list<string>();
       Schema.DescribeFieldResult fr = opportunity.stageName.getDescribe();
      
       for(Schema.PicklistEntry p  :  fr.getPicklistValues()){
        pv.add(p.getValue());
   }
         for(Integer i = 0; i < pv.size(); i++){
         String pvalue = pv[i];  
       listOfOpp = [select id, name, stagename, amount from opportunity where stagename = :pvalue];
       stagelist.add(listOfOpp);
         
   }

list<opportunity> prospecting = new list<opportunity>();
      prospecting =  stagelist[0];
       return prospecting;
      }
    } 

empucempuc

One more thing - as Davidjgriff pointed You haven't declared oppProspecting variable. Maybe because You've pasted only a code piece however could You please paste it's full version? 

 

And additional question - how Your FindScope class is related with page controller - StageOpp ?

wajidawajida

I have listed the code above to show you how i eventually managed to do it...but there is lot of code redundancy as you can see i copied and pasted the same for qualification, closed won etc and the code became quite long. I know that it is not the correct way to do it but to avoid the dereference a null object error i copied the same code from get opportunities to get prospecing. when you copy and paste the above code it will show you the list of opportunities and amount with stage prrospecting as a pageblock section. let  me know how can avoid writing the same code in get opportunities and get prospecting methods. in short how i can get the getopportunities mthod to execute first

 

Thanks

wajid

wajidawajida
see above