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
Navneeth RajNavneeth Raj 

System.NullPointerException: Attempt to de-reference a null object Class.CourseWrapperList.<init>: line 10, column 1

System.NullPointerException: Attempt to de-reference a null object Class.CourseWrapperList.<init>: line 10, column 1
SCENARIO: Create List of PickList options based on the courses available in the list?

And My Code for this is as below

CourseWrapper.apxc

public class CourseWrapper {
    public String cname {set;get;}
    public String ccode {get;set;}
    public CourseWrapper(String cname,String ccode)
    {
        this.cname=cname;
        this.ccode=ccode;
    }
}

CourseWrapperList.apxc
public class CourseWrapperList {
    public List<SelectOption> selected {set;get;}
    public List<CourseWrapper> courses {get;set;}
    CourseWrapper c1=new CourseWrapper('None','n');
    CourseWrapper c2=new CourseWrapper('Java','j-01');
    CourseWrapper c3=new CourseWrapper('Sfdc','sf-01');
    CourseWrapper c4=new CourseWrapper('Oracle','o-01');
    CourseWrapper c5=new CourseWrapper('Dotnet','dn-01');
    public CourseWrapperList(){
    courses.add(c1);
    courses.add(c2);
    courses.add(c3);
    courses.add(c4);
    courses.add(c5);
        so();
       }
    public pageReference so(){
    List<SelectOption> options=new List <SelectOption>();
        for(CourseWrapper c:courses){
        SelectOption op=new SelectOption(c.cname,c.ccode);
        options.add(op);
    }
          return null;
  }
}

CourseWrapperVfp.vfp
<apex:page controller="CourseWrapperList">
    <apex:form>
        <apex:pageBlock title="Courses">
            <apex:selectList value="{!selected}">
                <apex:selectOptions value="{!courses}"/>
            </apex:selectList>
        </apex:pageBlock>
        {!selected}
    </apex:form>
</apex:page>

But following (Image) Visualforce page error occurs. What is the error, How & why it occurs & how to overcome it?

User-added image
parth jollyparth jolly
Where have you updated a wrapper list???? i guess that why error is flashing .Till you will not update a list and the value in it will be zero .
 
Navneeth RajNavneeth Raj
Newbie asking. How to do that?
Suraj GharatSuraj Gharat
Hi,

In your controller's constructor you need to instantiate list of "CourseWrapper" first before you add elements into it. Add below line in your constructor as its first line.
 
courses=new List<CourseWrapper>();

HTH,
Suraj
parth jollyparth jolly
instead of return nulll just place it by options as its your list name and try?
 
Navneeth RajNavneeth Raj
Another error:
Invalid selectOptions found. Use SelectOption type in Apex
Suraj GharatSuraj Gharat
Change your controller to :
 
public class CourseWrapperList {
    public String selected {set;get;}
    public List<CourseWrapper> courses {get;set;}
    CourseWrapper c1=new CourseWrapper('None','n');
    CourseWrapper c2=new CourseWrapper('Java','j-01');
    CourseWrapper c3=new CourseWrapper('Sfdc','sf-01');
    CourseWrapper c4=new CourseWrapper('Oracle','o-01');
    CourseWrapper c5=new CourseWrapper('Dotnet','dn-01');
    public CourseWrapperList(){
   courses=new List<CourseWrapper>();
    courses.add(c1);
    courses.add(c2);
    courses.add(c3);
    courses.add(c4);
    courses.add(c5);
       }
    public List<SelectOption> getCourseAsSelectOptions(){
    List<SelectOption> options=new List <SelectOption>();
        for(CourseWrapper c:courses){
        SelectOption op=new SelectOption(c.cname,c.ccode);
        options.add(op);
    }
          return options;
  }
}

And change your VF to :
 
<apex:page controller="CourseWrapperList">
    <apex:form>
        <apex:pageBlock title="Courses">
            <apex:selectList value="{!selected}">
                <apex:selectOptions value="{!courseAsSelectOptions}"/>
            </apex:selectList>
        </apex:pageBlock>
        {!selected}
    </apex:form>
</apex:page>