2

I receive an error on a web page : Attempt to de-reference a null object

Schema.DescribeSObjectResult sObjResult = GLOBAL_DESCRIBE.get(sObjName).getDescribe();

First i thought that my user doesn't have the rigth CRUD access to the object "Job_Match__c" but my user is the administrator of the org.

Here is the call stack I receive and after the code that cause the error. I had the debug log after.

Visualforce Error

System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!initialize}' in component in page xyz:startjobmatch: Class.xxx.FLSCheck.hasPermissionObject: line 14, column 1 Class.xyz.SecurityHelper.validatePermissions: line 121, column 1 Class.xyz.SecurityHelper.allowOrDie: line 101, column 1 Class.xyz.StartJobMatchController.initialize: line 67, column 1 Class.xyz.FLSCheck.hasPermissionObject: line 14, column 1 Class.xyz.SecurityHelper.validatePermissions: line 121, column 1 Class.xyz.SecurityHelper.allowOrDie: line 101, column 1 Class.xyz.StartJobMatchController.initialize: line 67, column 1

code for Class.xyz.SecurityHelper.validatePermissions :

private static void validatePermissions(String sObjName,
                           List<String> fieldNames, String operationType)
  {
        if (!FLSCheck.hasPermissionObject(sObjName, operationType))
        {
            throwException(FLSCheck.GLOBAL_DESCRIBE.get(sObjName).getDescribe(), 
                                                     null, operationType);
        }

        if (fieldNames != null)
        {
            validateFieldsPermissions(sObjName, fieldNames, operationType);
        }
  }

code for Class.xxx.FLSCheck.hasPermissionObject

public with sharing class FLSCheck 
{
  public static final Map<String, Schema.SObjectType> GLOBAL_DESCRIBE = Schema.getGlobalDescribe();

    public static final String READ_ACCESS = 'Read';
    public static final String CREATE_ACCESS = 'Create';
    public static final String EDIT_ACCESS = 'Edit';
    public static final String DELETE_ACCESS = 'Delete';
    public static final String UPSERT_ACCESS = 'Upsert';

    public static Boolean hasPermissionObject(String sObjName, String operationType)
    {
      System.debug('sObjName '+sObjName);
        Schema.DescribeSObjectResult sObjResult = GLOBAL_DESCRIBE.get(sObjName).getDescribe();
        Boolean hasPermission = hasPermission(sObjResult, null, operationType);
        return hasPermission;
    }

Debug log :

21:58:39.0 (266265385)|SYSTEM_METHOD_EXIT|[3]|Schema.getGlobalDescribe() 21:58:39.0 (266421749)|VARIABLE_ASSIGNMENT|[3]|FLSCheck.GLOBAL_DESCRIBE|"{acceptedeventrelation=AcceptedEventRelation, account=Account, }"|0x874a1ce [1]|01p610000024TMO|SecurityHelper.SecurityHelper() [67]|01p610000024TMO|jobou363gmail.SecurityHelper.allowOrDie(String, String) 21:58:39.0 (268531485)|VARIABLE_ASSIGNMENT|[99]|sObjName|"Job_Match__c" [99]|operationType|String|false|false 21:58:39.0 (268538785)|VARIABLE_ASSIGNMENT|[99]|operationType|"Create" 21:58:39.0 (268548126)|HEAP_ALLOCATE|[101]|Bytes:5 [101]|01p610000024TMO|jobou363gmail.SecurityHelper.validatePermissions(String, List, String) 21:58:39.0 (268697345)|VARIABLE_ASSIGNMENT|[118]|sObjName|"Job_Match__c" [118]|fieldNames|List|true|false 21:58:39.0 (268710089)|VARIABLE_ASSIGNMENT|[118]|fieldNames|null|[118]|operationType|String|false|false 21:58:39.0 (268717042)|VARIABLE_ASSIGNMENT|[118]|operationType|"Create"[121]|Bytes:5[121]|01p610000024TLs|jobou363gmail.FLSCheck.hasPermissionObject(String, String) 21:58:39.0 (268778703)|VARIABLE_ASSIGNMENT|[11]|sObjName|"Job_Match__c" 21:58:39.0 (268784964)|VARIABLE_ASSIGNMENT|[11]|operationType|"Create" 21:58:39.0 (268888129)|USER_DEBUG|[13]|DEBUG|sObjName Job_Match__c [14]|Map.get(Object) [121]|01p610000024TLs|jobou363gmail.FLSCheck.hasPermissionObject(String, String) [101]|01p610000024TMO|jobou363gmail.SecurityHelper.validatePermissions(String, List, String) [67]|01p610000024TMO|jobou363gmail.SecurityHelper.allowOrDie(String, String) 21:58:39.0 (269078313)|SYSTEM_MODE_EXIT|false 21:58:39.0 (269166521)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.xyz.FLSCheck.hasPermissionObject: line 14, column 1 Class.xyz.SecurityHelper.validatePermissions: line 121, column 1 Class.xyz.SecurityHelper.allowOrDie: line 101, column 1 Class.xyz.StartJobMatchController.initialize: line 67, column 1 21:58:39.0 (269183992)|CODE_UNIT_FINISHED|StartJobMatchController invoke(initialize)

4

The error message just describes the situation where the object name you passed in wasn't found in the describe map and your code assumes it is always going to be found and is trying to call getDescribe() on null.

Writing it like this should resolve at least the exception. You should handle the scenario appropriately for your situation.

public static Boolean hasPermissionObject(String sObjName, String operationType) {
    System.debug('sObjName '+sObjName);

    // Get the item from the map
    Schema.SObjectType objType = GLOBAL_DESCRIBE.get(sObjName);

    // make sure we got an object token
    if (objType == null) {
        // bad object name?
        system.debug('The thing couldn\'t be found. Cannot call getDescribe');
        return null;

    } else {

        Schema.DescribeSObjectResult sObjResult = objType.getDescribe();
        Boolean hasPermission = hasPermission(sObjResult, null, operationType);
        return hasPermission;
    }

}
  • 2
    I recently found an example of how this can happen: if the version of the class doesn't support that object, it won't appear in the schema. For forwards compatibility, you should always check for null first. – sfdcfox Apr 18 '16 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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