+ Start a Discussion
Keaton KleinKeaton Klein 

Error on controller extension

Alright, I have a controller which works without errors but I am attempting to use it as an extension to the custom controller for a VF page.  When I update the VF page to use the standard controller I get the following error.

Visualforce Error
System.NullPointerException: Attempt to de-reference a null object
Class.ezwf.objectController.getObjectFields: line 59, column 1

I have posted the extension and the VF page below.  Any tips would be greaty appreciated.

 
public with sharing class testme {
private final Auto_ab__c ezab;
    public testme(ApexPages.StandardController stdController) {
        this.ezab = (Auto_ab__c)stdController.getRecord();
    }
    
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}
    
    public Auto_ab__c testContact1a;
    
    public String AutoabName {get; set;}
    
    public String TriggerValue {get; set;}
    
    public String abTemplateID {get; set;}
    
    public String selectedField {get; set;}
    
    public String selectedab {get; set;}
    
    public String templatename {get; set;}
    
    public String addobject {get; set;}
    
    public String editid {get; set;}

    public String abid;
    
    
    Public testme()
    {   
        selectedObject = 'account';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }
    
    ///////////
    
        
    ///////////

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;        
      }
         
    public List<SelectOption> getObjectabs() 
     {
                         List<SelectOption> abNames = new List<SelectOption>();
         for (abTemplate abName: [select id, name from abtemplate]) 
            {  
                abNames.add(new SelectOption(abname.id,abname.name));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return abNames;
     }
    
    /*
    public string getTemplateName()
    {
        
        
    }
        */

    
    public List<Selectoption> getAllCustomSObjects()
{
    List<Selectoption> sObjectList = new List<Selectoption>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
    {
        Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
        if(describeSObjectResultObj.isCustom())
        {
            String name = objTyp.getDescribe().getName();
            // Exclude all the unwanted Sobjects e.g. History, Share etc..
            if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
             !name.containsignorecase('share') && !name.containsignorecase('feed'))
            {
                //SobjectList.add(name);
                SobjectList.add(new SelectOption(name,name));
            }
        }
    }
    system.debug('SObjectList****' + SObjectList);
    return SObjectList;
}
public PageReference save() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             insert newdata;
            return Page.Easyab;
    }
    
    public PageReference edit() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(id=ApexPages.currentPage().getParameters().get('id'), name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             update newdata;
            return Page.Easyab;
    }
    
}
 
<apex:page standardcontroller="Auto_ab__c" extensions="objectcontroller">
<apex:form > 
      <apex:pageBlock >
<apex:pageBlockSection columns="2">
    <apex:pageBlockSectionItem >
        <apex:outputlabel value="Name/Description:"/>
                  <apex:inputText value="{!AutoabName}" />
    </apex:pageBlockSectionItem>
                  <apex:pageBlockSectionItem >
                  <apex:outputlabel value="Object Names :"/> 
                      <apex:actionRegion >
                           	<apex:selectList value="{!selectedObject}" size="1">
                            <apex:selectOption itemValue="Contact" itemLabel="Contact"/>
            				<apex:selectOption itemValue="Lead" itemLabel="Lead*" />
                            <apex:actionSupport event="onchange" rerender="myFields"/>
                            </apex:selectList>
                     </apex:actionRegion>                         
              </apex:pageBlockSectionItem>
          </apex:pageBlockSection>

<apex:pageBlockSection columns="2">

    

              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="Field Names :"/>   
                      <apex:outputPanel id="myFields">   
                        <apex:actionRegion >  
                           <apex:selectList value="{!selectedField}" size="1">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>
              
            <apex:pageBlockSectionItem >
                <apex:outputlabel value="Trigger Value:"/>
                          <apex:inputText value="{!TriggerValue}" />
            </apex:pageBlockSectionItem>
              
          </apex:pageBlockSection>
          
<apex:pageBlockSection columns="2">

    

              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="ab Names :"/>   
                      <apex:outputPanel id="myabs">   
                        <apex:actionRegion >  
                           <apex:selectList value="{!selectedab}" size="1">
                                <apex:selectOptions value="{!objectabs}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>

              
          </apex:pageBlockSection>
          
                    <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!edit}" value="Edit"/>
      </apex:pageBlockButtons>
      </apex:pageBlock>

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

 
Best Answer chosen by Keaton Klein
Amit Chaudhary 8Amit Chaudhary 8
Hi Keaton,

Please try below code.
public with sharing class testme {
private final Auto_ab__c ezab;
    public testme(ApexPages.StandardController stdController) {
        this.ezab = (Auto_ab__c)stdController.getRecord();
		        selectedObject = 'account';

	}
    
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}
    
    public Auto_ab__c testContact1a;
    
    public String AutoabName {get; set;}
    
    public String TriggerValue {get; set;}
    
    public String abTemplateID {get; set;}
    
    public String selectedField {get; set;}
    
    public String selectedab {get; set;}
    
    public String templatename {get; set;}
    
    public String addobject {get; set;}
    
    public String editid {get; set;}

    public String abid;
    
    
    Public testme()
    {   
        selectedObject = 'account';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }
    
    ///////////
    
        
    ///////////

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;        
      }
         
    public List<SelectOption> getObjectabs() 
     {
                         List<SelectOption> abNames = new List<SelectOption>();
         for (abTemplate abName: [select id, name from abtemplate]) 
            {  
                abNames.add(new SelectOption(abname.id,abname.name));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return abNames;
     }
    
    /*
    public string getTemplateName()
    {
        
        
    }
        */

    
    public List<Selectoption> getAllCustomSObjects()
{
    List<Selectoption> sObjectList = new List<Selectoption>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
    {
        Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
        if(describeSObjectResultObj.isCustom())
        {
            String name = objTyp.getDescribe().getName();
            // Exclude all the unwanted Sobjects e.g. History, Share etc..
            if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
             !name.containsignorecase('share') && !name.containsignorecase('feed'))
            {
                //SobjectList.add(name);
                SobjectList.add(new SelectOption(name,name));
            }
        }
    }
    system.debug('SObjectList****' + SObjectList);
    return SObjectList;
}
public PageReference save() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             insert newdata;
            return Page.Easyab;
    }
    
    public PageReference edit() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(id=ApexPages.currentPage().getParameters().get('id'), name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             update newdata;
            return Page.Easyab;
    }
    
}

For controller we can use without parameterized constructor. 
For Standard controller usse parameterized constructor
Please let us kn ow if this will help u

Thanks
Amit Chaudhary

All Answers

William TranWilliam Tran
That error usually mean you have NOT initialize your variables (they are nulls).

For example:
public String AutoabName {get; set;}

AutoabName is currently null and you need to initialize before accessing it.

So initialize in your constructor.

 Public testme()

    {  

        selectedObject = 'account';

    AutoabName ='';

    }

you initialized SelectedObject, you might want consider initialize all of these too.
 
    public String selectedObject {get; set;}
    
    public Auto_ab__c testContact1a;
    
    public String AutoabName {get; set;}
    
    public String TriggerValue {get; set;}
    
    public String abTemplateID {get; set;}
    
    public String selectedField {get; set;}
    
    public String selectedab {get; set;}
    
    public String templatename {get; set;}
    
    public String addobject {get; set;}
    
    public String editid {get; set;}

    public String abid;

Thx
Keaton KleinKeaton Klein
Hi William, thank you so much for the quick reply.  A followup question or you.  I have a map on line 58

Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();

How would you go about initializing fieldmap?
William TranWilliam Tran

Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();

It's already initialized upon instantiation.

Usually when you see the "=" that means it mapped to something, in other words, initialized (if it is the first time)

Thx
Amit Chaudhary 8Amit Chaudhary 8
Hi Keaton,

Please try below code.
public with sharing class testme {
private final Auto_ab__c ezab;
    public testme(ApexPages.StandardController stdController) {
        this.ezab = (Auto_ab__c)stdController.getRecord();
		        selectedObject = 'account';

	}
    
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}
    
    public Auto_ab__c testContact1a;
    
    public String AutoabName {get; set;}
    
    public String TriggerValue {get; set;}
    
    public String abTemplateID {get; set;}
    
    public String selectedField {get; set;}
    
    public String selectedab {get; set;}
    
    public String templatename {get; set;}
    
    public String addobject {get; set;}
    
    public String editid {get; set;}

    public String abid;
    
    
    Public testme()
    {   
        selectedObject = 'account';
    }

    public List<SelectOption> getObjectNames() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
        {
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
     }
    
    ///////////
    
        
    ///////////

     public List<SelectOption> getObjectFields() 
     {
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            List<SelectOption> fieldNames = new List<SelectOption>();
            for (String fieldName: fieldMap.keySet()) 
            {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return fieldNames;        
      }
         
    public List<SelectOption> getObjectabs() 
     {
                         List<SelectOption> abNames = new List<SelectOption>();
         for (abTemplate abName: [select id, name from abtemplate]) 
            {  
                abNames.add(new SelectOption(abname.id,abname.name));
              //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
            }
            return abNames;
     }
    
    /*
    public string getTemplateName()
    {
        
        
    }
        */

    
    public List<Selectoption> getAllCustomSObjects()
{
    List<Selectoption> sObjectList = new List<Selectoption>();
    for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values())
    {
        Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe();
        if(describeSObjectResultObj.isCustom())
        {
            String name = objTyp.getDescribe().getName();
            // Exclude all the unwanted Sobjects e.g. History, Share etc..
            if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
             !name.containsignorecase('share') && !name.containsignorecase('feed'))
            {
                //SobjectList.add(name);
                SobjectList.add(new SelectOption(name,name));
            }
        }
    }
    system.debug('SObjectList****' + SObjectList);
    return SObjectList;
}
public PageReference save() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             insert newdata;
            return Page.Easyab;
    }
    
    public PageReference edit() {   
    Contact testContact1a = new Contact(LastName='TestContact',  description = 'TestContact');
    //public String templatename { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
             string templatename = [select name from abtemplate where id =:Selectedab].name;
             Auto_ab__c newdata = new Auto_ab__c(id=ApexPages.currentPage().getParameters().get('id'), name=AutoabName, trigger_value_1__c=TriggerValue, Object_1__c=selectedObject,  Trigger_Field_1__c = selectedField, ab_Template_ID__c=Selectedab, ab_Template_name__c=TemplateName );
             update newdata;
            return Page.Easyab;
    }
    
}

For controller we can use without parameterized constructor. 
For Standard controller usse parameterized constructor
Please let us kn ow if this will help u

Thanks
Amit Chaudhary
This was selected as the best answer
Keaton KleinKeaton Klein
Thanks Amit, that did the trick for me.  And thank you as well William, very much appreciated.
William TranWilliam Tran
Keaton,  glad it all worked out for you.

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

This will help keep the forum clean and help future users determine what answers are useful
and what answer was the best in resolving the user's issue. 

Thanks