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
p100p100 

Set initial value of Multiselect list - Attempt to dereference a null object

Hi

 

On a VF page that produces a pdf report I have a multiselect list of users that i want to default to the logged in user if they have a particular profile, however, i am getting the error "Attempt to dereference a null object" on the following line:

 

surveyor.add(userinfo.getUserId());

 

The code im using is below, grateful for any pointers where im going wrong.

 

VF:

 

<apex:selectList id="Surveyors" size="1" title="Surveyor" value="{!surveyor}" multiselect="true" style="height:107px">
            <apex:selectOptions value="{!Surveyors}"></apex:selectOptions>
</apex:selectList> 

 Apex:

    public List<String> surveyor;
    public List<String> getSurveyor()
    {
        List<Profile> profile = [Select id,Name from Profile Where Id=:userinfo.getProfileId() limit 1];
            string userProfile = profile[0].Name;
            if(userProfile == 'Surveyor')
            {
                surveyor.add(userinfo.getUserId());
            }
        return surveyor;
    }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sgt_KillerSgt_Killer

Try this out -

 

public List<String> surveyor = new List<String>();
    public List<String> getSurveyor()
    {
        List<Profile> profile = [Select id,Name from Profile Where Id=:userinfo.getProfileId() limit 1];
            string userProfile = profile[0].Name;
            if(userProfile == 'Surveyor')
            {
                surveyor.add(userinfo.getUserId());
            }
        return surveyor;
    }

All Answers

Sgt_KillerSgt_Killer

Try this out -

 

public List<String> surveyor = new List<String>();
    public List<String> getSurveyor()
    {
        List<Profile> profile = [Select id,Name from Profile Where Id=:userinfo.getProfileId() limit 1];
            string userProfile = profile[0].Name;
            if(userProfile == 'Surveyor')
            {
                surveyor.add(userinfo.getUserId());
            }
        return surveyor;
    }
This was selected as the best answer
Puja_mfsiPuja_mfsi

Hi,

We can create custom picklist in VF using SelectOption .you need to add value in selectOption list .

VF

<apex:selectList id="Surveyors" size="1" title="Surveyor" value="{!surveyor}" multiselect="true" style="height:107px">

     <apex:selectOptions value="{!Surveyors}"></apex:selectOptions>
 </apex:selectList>
      

Controller

public List<String> surveyor{get;set;}
public List<SelectOption> getSurveyors(){
      List<SelectOption> options = new List<Selectoption>();
      List<Profile> profile = [Select id,Name from Profile Where Id=:userinfo.getProfileId() limit 1];
      string userProfile = profile[0].Name;
       if(userProfile == 'Surveyor'){
               options.add(new SelectOption(userinfo.getUserId(),userinfo.getUserId()));
        }
        return options;
}

 

Please let me know if u have any problem on same and if this post helps u plz give kudos by click on star at left.