I am getting this error when I press the save2 command button in the plus sign Attempt to de-reference a null object Error is in expression '{!submitLead}' in page permissionsinspector: Class.myEmployeeController3.submitLead: line 20, column 1

An unexpected error has occurred. Your development organization has been notified. Here is my visaulforce page

<apex:page sidebar="false" showHeader="false" controller="myEmployeeController3">
    <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
        <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
        <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />

            p { font-weight: bold; }
            p {font-size: 15px;}
            h1,h2 {display:block;
            text-align: center;
            margin: 5px;
            margin-top: 10px;}
            h1 {font-size: 20px;}
            h2 {font-size: 16px;}
            #inputSection {text-align: center;
            margin-bottom: 10px;}

        function switchMenu(obj,obj1,obj2) 
            var el = document.getElementById(obj);                                       
            if ( el.style.display != 'none' ) {
                el.style.display = 'none';
            else {
                el.style.display = '';
            var e2 = document.getElementById(obj1);                                       
            if ( e2.style.display != 'none' ) {
                e2.style.display = 'none';
            else {
                e2.style.display = '';
            var e3 = document.getElementById(obj2);                                       
            if ( e2.style.display != 'none' ) {
                e3.style.display = 'none';
            else {
                e3.style.display = '';

        <apex:form >
            <h1>Permissions Inspector</h1>
            <h2>Enter Object API Name</h2>

            <div id="inputSection">
                <apex:inputText value="{!SObjectName}"/>
                <apex:commandButton action="{!actionSupMethod}" reRender="test2" value="Submit"/>
            <apex:actionfunction action="{!submitLead}" name="myFunction" reRender="PBS1">
                <apex:param name="recordId" value="" assignTo="{!recordId}"/>
            <apex:pageBlock >
                <apex:pageBlockTable id="test2" value="{!objectout}" var="cus" >
                    <apex:column >
                        <apex:facet name="header">                                        
                            THE RESULTS ARE                                                                             
                        <apex:outputpanel id="plusimage">
                            <apex:image url="{!$Resource.Plus_Image}" onclick="switchMenu
                                                                               ('{!$Component.inlinetablesec}','{!$Component.minusimage}','{!$Component.plusimage}')" title="Expand"/> 
                        <apex:outputpanel id="minusimage" style="display:none;">
                            <apex:image url="{!$Resource.Minus_Image}"
                                        onclick="switchMenu('{!$Component.inlinetablesec}','{!$Component.plusimage}','{!$Component.minusimage}')" title="Collapse"/>

                        <apex:outputpanel id="inlinetablesec" style="display:none;">
                            <apex:variable value="{!0}" var="rowNum"/>
                            <apex:repeat var="count" value="{!cus.Parent.Profile.Name}">
                                <apex:variable var="rowNum" value="{!rowNum+1}"/>
                                <apex:commandButton value="Save2" onclick="myFunction('{!cus.ParentId}'); return false;"/>
                            <apex:outputText rendered="{!rowNum=0}">  Element Set Null </apex:outputText>

                            <apex:pageblocktable value="{!dataMap}" var="tm" rendered="{!rowNum>0}" id="PBS1">
                                <apex:column headerValue="Field names" value="{!dataMap[tm].Field}"/>

                    <apex:column headerValue="Object" value="{!cus.SObjectType}"/>
                    <apex:column headerValue="Read" value="{!cus.PermissionsRead}"/>
                    <apex:column headerValue="Parent ID" value="{!cus.ParentId}"/> 
                    <apex:column headerValue="Edit" value="{!cus.PermissionsEdit}"/>p
                    <apex:column headerValue="Delete" value="{!cus.PermissionsDelete}"/>
                    <apex:column headerValue="ViewAll" value="{!cus.PermissionsViewAllRecords}"/> 
                    <apex:column headerValue="ModifyAll" value="{!cus.PermissionsModifyAllRecords}"/>
            <!-- <apex:pageBlock title="Parameters">
<apex:pageBlockSection id="PBS1">
Passed parameter in Form: {!recordId} <br/>
<apex:pageBlock >
<apex:pageBlockTable id="test3" value="{!fieldPerms}" var="cus1" >
<apex:column headerValue="Field names" value="{!cus1.Field}"/>


Here's the controller

public with sharing class myEmployeeController3 {
    public String SObjectName{get;set;}
    public List<ObjectPermissions> objectout{get;set;}
    public List<FieldPermissions> fieldPerms{get;set;}
    public String recordId{get; set;}

    public Map<ObjectPermissions,List<FieldPermissions>> dataMap{get; set;}
     Map<Id,ObjectPermissions> objMap = new Map<Id,ObjectPermissions>();

    public PageReference submitLead() {
        fieldPerms = [SELECT  Field, ParentId FROM FieldPermissions
                      WHERE  ParentId=:recordId AND SObjectType =:SObjectName 
                      ORDER BY Field DESC];

        if(fieldPerms != null)
            for(FieldPermissions fp :fieldPerms)
                    dataMap.put(objMap.get(fp.ParentId),new List<FieldPermissions>());
        //catch (System.NullPointerException e) {

        return null;
    public myEmployeeController3(){
        recordId= '';
    Public void actionSupMethod(){
        objectout =  [SELECT ParentId, Parent.Profile.Name, SObjectType, PermissionsRead, PermissionsCreate,PermissionsEdit,PermissionsDelete,
                      FROM ObjectPermissions 
                      WHERE SObjectType = :SObjectName AND (PermissionsRead = true OR PermissionsCreate = true 
                                                            OR PermissionsEdit = true OR PermissionsDelete = true 
                                                            OR PermissionsViewAllRecords = true OR PermissionsModifyAllRecords = true)
                      ORDER BY Parent.IsOwnedByProfile ASC];

        if(objectout != null)
            for(ObjectPermissions op :objectout)


  • 1
    It appears one of your maps is == null. Have you confirmed these maps are populated prior to the submitLead method call? My first guess would be dataMap since objMap is initialized as an empty map (would not throw a null-pointer exception). Commented Apr 28, 2016 at 15:19
  • 1
    N.B. fieldPerms will never be null - if no records found, it will be an empty list - the if test is unnecessary
    – cropredy
    Commented Apr 28, 2016 at 15:22

1 Answer 1


Your dataMap variable is never instantiated. Before your loop, instantiate it as empty. You should also use the ParentId as your map key. One of my colleagues once wrote a generic utility called GroupBy that performs this sort of functionality. Might be worth investing some time into. It's pretty intuitive to build out.

dataMap = new Map<Id, List< FieldPermissions>>(); // MISSING
for(FieldPermissions fp :fieldPerms)
     if (!dataMap.containsKey(fp.ParentId))
        dataMap.put(fp.ParentId, new List< FieldPermissions>());

If you want to build out this utility method specifically, it would look like:

public class GroupBy
    public Map<Id, List<SObject>> ids(SObjectField lookup, List<SObject> records)
        Map<Id, List<SObject>> grouped = new Map<Id, List<SObject>>();
        for (SObject record : records)
            Id reference = (Id)record.get(lookup);
            if (!grouped.containsKey(reference))
                grouped.put(reference, new List<SObject>());
        return grouped;

If you implement the above, you can simply assign the method result directly to dataMap.

dataMap = GroupBy.ids(FieldPermissions.ParentId, fieldPerms)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .