+ Start a Discussion
Manoj Goswami 5Manoj Goswami 5 

Create DropDown list in Visualforce by taking values from Controller (getting unexpected error)

I want a list of all Objects in my org and displaying it in a drop down in Visualforce page. The code has compiled but giving unexpected error while getting Preview of VF page.

Controller : 
// Controller
public without sharing class getObjectNamesController {    
    
    public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe(); 
    public Set<String> objNamesSet = object_Map.keyset();
    
    public List<SelectOption> objNamesList{     
        get{
            Integer i=0;
            for(String myObjName : objNamesSet)
            {
                objNamesList.add(new SelectOption(String.valueOf(i),myObjName)) ; 
                i++;
            } 	    	
            System.debug(objNamesList);   
            return objNamesList ;
        }
        set;
    }
}
VF Page: 
<apex:page controller="getObjectNamesController">
  <apex:form >
      <apex:selectList size="1">
          <apex:selectOptions value="{!objNamesList}"></apex:selectOptions>
      </apex:selectList>
  </apex:form>
</apex:page>
User-added image

 
Best Answer chosen by Manoj Goswami 5
Arpit Jain7Arpit Jain7
Hello Manoj,

Update below code in your controller class

// Controller
public without sharing class getObjectNamesController {    
    
    public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe(); 
    public Set<String> objNamesSet = object_Map.keyset();
    
    public List<SelectOption> objNamesList{     
        get{
            List<SelectOption> options = new List<SelectOption>();
            Integer i=0;
            for(String myObjName : objNamesSet)
            {
                options.add(new SelectOption(String.valueOf(i),myObjName)) ; 
                i++;
            }           
            System.debug(objNamesList);   
            return options;
        }
        set;
    }
}

Let me know for any issues.

Thanks
Arpit

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

All Answers

Sam1942Sam1942
Please put debug logs and check the line number where you are getting an error.
And why are you using  integer i here? you can simply do it like - objNamesList.add(new SelectOption(myObjName,myObjName)) ;
Arpit Jain7Arpit Jain7
Hello Manoj,

Update below code in your controller class

// Controller
public without sharing class getObjectNamesController {    
    
    public Map<String, Schema.SObjectType> object_Map = Schema.getGlobalDescribe(); 
    public Set<String> objNamesSet = object_Map.keyset();
    
    public List<SelectOption> objNamesList{     
        get{
            List<SelectOption> options = new List<SelectOption>();
            Integer i=0;
            for(String myObjName : objNamesSet)
            {
                options.add(new SelectOption(String.valueOf(i),myObjName)) ; 
                i++;
            }           
            System.debug(objNamesList);   
            return options;
        }
        set;
    }
}

Let me know for any issues.

Thanks
Arpit

Please mark this answer as SOLVED and BEST ANSWER if it helps you.
This was selected as the best answer
Manoj Goswami 5Manoj Goswami 5
HI , could you tell me why did you created List named "options" inside the get block instead of outside. It would be better if explained analogy of it. Thanks
Arpit Jain7Arpit Jain7
You can add it outside get block also, It does not metter. The main thing is before adding any item to list (Collection) you need to Intialize it.

Thanks
Arpit

Please mark this answer as SOLVED and BEST ANSWER if it helps you.