1

I am trying to get sObject fields name in 2nd pick list on the basis of selected object name in 1st pick list.

Here is my class..

public with sharing class ExtractSobject{
    public list<SelectOption> fields { get; set; }

    public String objectName { get; set; }    

    public List<SelectOption> getSelectedobjnames()
      {
        List<Schema.SObjectType> obj = Schema.getGlobalDescribe().Values();
         List<SelectOption> options = new List<SelectOption>();
           options.add(new SelectOption('--Select Object--','--Select Object--'));
            for(Schema.SObjectType st : obj)
              {
               options.add(new SelectOption(st.getDescribe().getName(),st.getDescribe().getName()));
              }
               return options;
       }
         public String Sf{get;set;}
         public List<SelectOption> objFields{get; set;}

         public List<SelectOption> getSelectedobjFields()
      {
        SObjectType objTyp = Schema.getGlobalDescribe().get('Selectedobjnames');
        DescribeSObjectResult objDef = objTyp.getDescribe();
        Map<String, SObjectField> fields = objDef.fields.getMap();

        Set<String> fieldSet = fields.keySet();
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select Object--','--Select Object--'));
          for(String s:fieldSet)
             {  
              SObjectField Sobjfields = fields.get(s);
              DescribeFieldResult selectedField = Sobjfields.getDescribe();                 
              options.add(new SelectOption(selectedField.getName(),selectedField.getName()));
              }
         return options;
       }
}

Page...

<apex:page controller="ExtractSobject">
<apex:form >
   <apex:pageblock >
     <apex:pageblocksection >
       <apex:pageBlockSectionItem >
         <apex:outputlabel value="Select Object"/>
          <apex:selectList value="{!fields}" size="1">
            <apex:selectoptions value="{!Selectedobjnames}"></apex:selectoptions>
            <apex:actionSupport event="onchange" rerender="a"/>
         </apex:selectList>
       </apex:pageBlockSectionItem>

     <apex:pageBlockSectionItem >
      <apex:outputPanel id="a">
         <apex:outputLabel value="Object Fields" ></apex:outputLabel>
             <apex:selectList value="{!Sf}" size="1"> 
                   <apex:selectOptions value="{!SelectedobjFields}" />
             </apex:selectList>
      </apex:outputPanel>
     </apex:pageBlockSectionItem>


   </apex:pageBlockSection>   
  </apex:pageblock>
 </apex:form>

</apex:page>

I am getting an exception

Attempt to de-reference a null object

3
  • Is selectedobjnames literally the name of the object you're trying to describe? That would cause a NullPointerException.
    – sfdcfox
    Commented Jul 28, 2015 at 5:02
  • Actually selectedobjnames is not an object it is the name of function which store all sobject name in pick-list... Commented Jul 28, 2015 at 5:04
  • your code's Schema.getGlobalDescribe().get('Selectedobjnames') this line return null that's why you are facing this error. you have to specify object name in get method.. Commented Jul 28, 2015 at 6:27

3 Answers 3

2

In this line

 SObjectType objTyp = Schema.getGlobalDescribe().get('Selectedobjnames');
 DescribeSObjectResult objDef = objTyp.getDescribe();

You are giving 'Selectedobjnames' as a string. you need to pass it without single quote. I think it will solve your problem.

1
  • Because there is NO OBJECT NAMED SelectedObjNames
    – Eric
    Commented Jul 28, 2015 at 14:07
1

Seems time-consuming for us to read through the code and locate the issue. So instead, I will provide you some info on how to debug this one.

For VF page, enable development mode for your current user: How to enable development mode. Refresh your page. Now you should get the detailed stack trace. That would hopefully help you resolving your issue

0
0

As a start

You cannot use the selected objectnames in your code as a value as the getter always return a new list.

Based on how you currently have you will need to use the property in your controller you already have objectName:

SObjectType objTyp = Schema.getGlobalDescribe().get(objectName);

Then change your VF page with this:

<apex:selectList value="{!objectName}" size="1">
            <apex:selectoptions value="{!Selectedobjnames}"></apex:selectoptions>
            <apex:actionSupport event="onchange" rerender="a"/>
         </apex:selectList>
2
  • Thank you Eric but still getting the same exception.. Commented Jul 28, 2015 at 5:55
  • then objectName is not being populated.
    – Eric
    Commented Jul 28, 2015 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .