Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to get fields from Contact into fieldset. but i am not bale to get the fields properly i am getting error : Attempt to de-reference a null object

my code :

     public class DemoCtrl{

     public contact DemoCtrl { get; set; }

     public List<schema.fieldsetmember> getACTFields(){ 
             // System.debug('getField ' + Fields);     
             return getFields('contact', 'ACT');

       public static List<schema.fieldsetmember> getFields(String objectName, String fieldSetName) 

        Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); 
        Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(objectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();            
         Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);

          System.debug('GlobalDescribeMap ' + GlobalDescribeMap);
         System.debug(' SObjectTypeObj  ' + SObjectTypeObj );
          System.debug('DescribeSObjectResultObj ' +          DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName)); 
         System.debug('fieldSetObj  ' + fieldSetObj ); 
        return fieldSetObj.getFields();        



I am getting null into DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName). Can anybody guide me what is the issue it is not getting filled.

share|improve this question
Either DescribeSObjectResultObj is null, or it doesn't contain fieldsets, making DescribeSObjectResultObj.FieldSets null. Can you put them in a System.Debug call? – Lex Jan 21 '13 at 9:29
the DescribeSObjectResultObj.FieldSetsFieldSets is through --> DescribeSObjectResultObj.FieldSetsFieldSets[Contact] in System.debug. And DescribeSObjectResultObj through --> contact – AnuRaj Jan 21 '13 at 9:35
Your code worked for me. Seems like the API name of your field set is not ACT. – Shailesh Deshpande Jan 21 '13 at 9:58
Can anybody tell me how to save the entered value into the object. I want to use a custom button on it. – AnuRaj Jan 21 '13 at 10:30
yes, you are right. I have solved it. can you tell me how to save the entered value into the object. I want to use a custom button on it – AnuRaj Jan 21 '13 at 10:32

Your code works for me. Like,

DemoCtrl.getFields('Contact', 'FieldSet_Name');

But if you use Field Label of FieldSet in the call, you will get the error.

By any chance, have your used a label name but not the API name to call your field set?

share|improve this answer
thanks. i have solved it. – AnuRaj Jan 21 '13 at 10:29
can you tell me how to save the entered value into the object. I want to use a custom button on it. – AnuRaj Jan 21 '13 at 10:32

If you put a System.debug on the fieldsetName and verify if the field set Name is correct, you may be able to find out the problem.

share|improve this answer
yes that i did it is getting -- > ACT – AnuRaj Jan 21 '13 at 9:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.