You need to sign in to do that
Don't have an account?
jai.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
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
you have to set the value for objectName.
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