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
jai.sjai.s 

System.NullPointerException: Attempt to de-reference a null object

Hi,

I am get error like: 
System.NullPointerException: Attempt to de-reference a null object
Class.SearchListController.getSObjectList: line 19, column 1     

public class SearchListController{
 
    public String objectName {get;set;}
    public List<String> objectFields {get;set;}
    public List<SObject> SObjectListToShow {get;set;}
    public SearchListController(){
        //objectName= 'MyObject__c';
        objectName= '';
        SObjectListToShow =new List<SObject>();
       // SObjectListToShow = getSObjectList();
    }
    
    public List<SObject> getSObjectList(){
    
    //Getting field list for the sObject 
        objectFields =  new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType sObjType = globalDescription.get(objectName);
        Schema.DescribeSObjectResult res = sObjType.getDescribe();                                // In this line, iam getting error
 
         Map<String , Schema.SObjectField> mapFieldList = res.fields.getMap();
        for(Schema.SObjectField field : mapFieldList.values())
        {
            Schema.DescribeFieldResult fieldResult = field.getDescribe();
            if(fieldResult.isAccessible())
            {
                objectFields.add(fieldResult.getName());
            }
        }
 
        //Building Query with the fields
        Integer i = 0;
        String fieldsToFetch = '';
        Integer len = objectFields.size();
        for(String temp:objectFields)
        {
 
            if(i==len-1)
            {
                  fieldsToFetch = fieldsToFetch + temp;
            }
            else
            {
                  fieldsToFetch = fieldsToFetch + temp + ',';
            }
            i++;
        }
            String qryStr = 'Select ' + fieldsToFetch + ' From ' + objectName  ;
            return  Database.Query(qryStr);
    }
}

VF Page:
<apex:page controller="SearchListController">
    <apex:form >
        <apex:pageBlock id="result" >
            <apex:pageBlockTable value="{!sObjectList}" var="res">
                <apex:repeat value="{!objectFields}" var="field">
                    <apex:column value="{!res[field]}"/>
                </apex:repeat>
            </apex:pageBlockTable>
        </apex:pageBlock>        
    </apex:form>
</apex:page>
Please help sove this problem.

Regards,
Shaik
BalajiRanganathanBalajiRanganathan
You are getting null because you have objectName= ''; in your constructor.  
you have to set the value for objectName.
SarfarajSarfaraj
Hi Shaik

You are getting the null pointer at line 19 because at line 18 objectName is blank(''). For this reason sObjType is null and this is causing the exception in the next line. However I am not clear abount what you want to achieve. If you need anything further please elaborate your requirement.

--Akram
AnaAEAnaAE
Can you help me please!!!!! I have similar problem