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
Pooja NekkalapudiPooja Nekkalapudi 

how to solve System.NullPointerException: Attempt to de-reference a null object Class.UserAccessDetailsController.getObjectLabels

 /*error: System.NullPointerException: Attempt to de-reference a null object 
Class.UserAccessDetailsController.getObjectLabels: line 290, column 1
line 290 is this describe.get(op.SObjectType).getDescribe().getLabel()));
*/

 public List<NameLabel> getObjectLabels() {
        if (null == xObjectLabels) {
            List<NameLabel> result = new List<NameLabel>();
            Map<String,SObjectType> describe = Schema.getGlobalDescribe();

            
            for (ObjectPermissions op : [SELECT SObjectType FROM ObjectPermissions 
                                          WHERE Parent.Profile.Name = 'System Administrator'  ]) {
                result.add(new NameLabel(op.SObjectType, 
                                         describe.get(op.SObjectType).getDescribe().getLabel()));
            }
            result.sort();
            xObjectLabels = result;
        }
        return xObjectLabels;
    }
Best Answer chosen by Pooja Nekkalapudi
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Pooja, 
Pls try this, it should work fine for you - 
for (ObjectPermissions op : [SELECT SObjectType FROM ObjectPermissions WHERE Parent.Profile.Name = 'System Administrator']) {
    if(op.SObjectType!=null && describe.get(op.SObjectType)!=null){
    	result.add(new NameLabel(op.SObjectType, describe.get(op.SObjectType).getDescribe().getLabel()));    
    }  
}
I checked - 
describe.get(op.SObjectType) is coming null.

Thanks, 
Sumit Kumar Singh

All Answers

@Karanraj@Karanraj
check whether op.sobjectType is returning any null value in the query.
for (ObjectPermissions op : [SELECT SObjectType FROM ObjectPermissions WHERE Parent.Profile.Name = 'System Administrator']) {
 System.debug( "value :" + op.SObjectType);
 if(op.SObjectType!= null){
   result.add(new NameLabel(op.SObjectType,describe.get(op.SObjectType).getDescribe().getLabel()));
  }
}                              

 
Pooja NekkalapudiPooja Nekkalapudi
This change gives me this error: Error: Compile Error: line 289:56 no viable alternative at character '"' at line 289 column 56
Pooja NekkalapudiPooja Nekkalapudi
this is line 289 
System.debug( "value :" + op.SObjectType); 
@Karanraj@Karanraj
It's my mistake, use single Quotation mark instead of double Quotation in the debug statement
System.debug( 'value :' + op.SObjectType);

 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Pooja, 
Pls try this, it should work fine for you - 
for (ObjectPermissions op : [SELECT SObjectType FROM ObjectPermissions WHERE Parent.Profile.Name = 'System Administrator']) {
    if(op.SObjectType!=null && describe.get(op.SObjectType)!=null){
    	result.add(new NameLabel(op.SObjectType, describe.get(op.SObjectType).getDescribe().getLabel()));    
    }  
}
I checked - 
describe.get(op.SObjectType) is coming null.

Thanks, 
Sumit Kumar Singh
This was selected as the best answer
Pooja NekkalapudiPooja Nekkalapudi
This worked Thanks Sumit but after modifying this piece I got 3 more errors
 
Error: System.NullPointerException: Attempt to de-reference a null object
 
Class.UserAccessDetailsController.getObjectPerms: line 361, column 1
Class.UserAccessDetailsController.getObjectPermStatus: line 317, column 1
Class.UserAccessDetailsController.__sfdc_flsObjectType: line 93, column 1
 
// this is for line 361// public Map<Id,Map<String,ObjectPermissions>> getObjectPerms() {
        if (null == xObjectPerms) {
            Map<Id,Map<String,ObjectPermissions>> result = new Map<Id,Map<String,ObjectPermissions>>();
            for (NameLabel ps : permsetInfo) {
                result.put(ps.Id, new Map<String,ObjectPermissions>());
                for (NameLabel op : getObjectLabels()) {
                    result.get(ps.Id).put(op.apiName, new ObjectPermissions());
                }
            }
            Map<String,ObjectPermissions> totalAccess = new Map<String,ObjectPermissions>();
            result.put(null, totalAccess);
            for (NameLabel op : getObjectLabels()) {
                totalAccess.put(op.apiName, new ObjectPermissions());
            }
            for (ObjectPermissions op : [SELECT Id, SObjectType, ParentId,
                                                PermissionsCreate, PermissionsRead, PermissionsEdit, PermissionsDelete,
                                                PermissionsViewAllRecords, PermissionsModifyAllRecords
                                           FROM ObjectPermissions
                                          WHERE ParentId IN :extractPsIds(permsetInfo)]) {
                Map<String,ObjectPermissions> opMap = result.get(op.ParentId);
                opMap.put(op.SObjectType, op);
                ObjectPermissions totalOp = totalAccess.get(op.SObjectType);
                for (String apiName : getObjectPermFieldNames()) {
//this is line 361//                    totalOp.put(apiName, getWithDefault(totalOp, apiName, false) || getWithDefault(op, apiName, false));
                }
            }
 
            xObjectPerms = result;
        }
        return xObjectPerms;
    }
 
//this is for line 317// public MapHolder getObjectPermStatus() {
        if (null == xObjectStatus) {
            Map<String,Boolean> msb = new Map<String, Boolean>();
            for (NameLabel nl : getObjectLabels()) {
               {
                msb.put(nl.apiName,
//this is line 317//                        getWithDefault(getObjectPerms().get(null).get(nl.apiName), READ, false)); }
            }
            xObjectStatus = new MapHolder(msb);
        }
        return xObjectStatus;
    }
Jitendra kumarJitendra kumar
List<ObjectPermissions > listOfObjectPermissions  = [SELECT SObjectType FROM ObjectPermissions WHERE Parent.Profile.Name = 'System Administrator']
if(listOfObjectPermissions.size()>0){
for (ObjectPermissions op :listOfObjectPermissions ) { System.debug( "value :" + op.SObjectType);  if(op.SObjectType!= null){ result.add(new NameLabel(op.SObjectType,describe.get(op.SObjectType).getDescribe().getLabel()));   } }
}

Try to check firt null before for loop.
Jitendra kumarJitendra kumar
  1. List<ObjectPermissions > listOfObjectPermissions  = [SELECT SObjectType FROM ObjectPermissions WHERE Parent.Profile.Name = 'System Administrator']
    if(listOfObjectPermissions.size()>0){
    for (ObjectPermissions op :listOfObjectPermissions ) { System.debug( "value :" + op.SObjectType);  if(op.SObjectType!= null){ result.add(new NameLabel(op.SObjectType,describe.get(op.SObjectType).getDescribe().getLabel()));   } }
    }

    Try to check firt null before for loop.