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
Sibanee purohitSibanee purohit 

Map key null not found in map error for apex:pageblockTable

Can anyone please help me with this problem.This error is showing only for PageblockTable.if I use apex:repeat it's working but I need to use pageblockTable. Error is 'Map key null not found in map Error is in expression '{!objRecordsMap[obj]}' in component'. Here is the code
VF Page:
<apex:page controller="FindRelatedData" >
<apex:form >
    <apex:pageBlock title="Related Data">
    <apex:pageblockSection>
    <apex:pageblockTable value="{!objFieldsMap}" var="obj">
        <!---{!obj}<br></br>-->
      <apex:repeat value="{!objRecordsMap[obj]}" var="record">
          
            <apex:repeat value="{!objFieldsMap[obj]}" var="field">
                <!---{!record[field]}<br></br>-->
                
              <apex:column headerValue="{!field}">
                  <apex:outputText value="{!record[field]}"/>
               </apex:column>
            </apex:repeat>
      </apex:repeat>

    </apex:pageblockTable>
  </apex:pageblockSection>
    </apex:pageBlock>  
    </apex:form> 
</apex:page>

Apex code:
public class FindRelatedData {

    public String getQueries() {
        return null;
    }
    public Id id = '0016F00001rZlrM';
    string objType;
    string childName;
    string fname;
    //string qry;
   Public List<string> fieldlist{get;set;}
   Public List<sObject> objQry{get;set;}
    List<sObject> queries;
    Map<String, Schema.SobjectField> fmap = new Map<String, Schema.SobjectField>();
    List<string> objNames = new List<string>();
    public List<SObject> childObjs = new List<SObject>();
    public Map<String, List<string>> objFieldsMap {get;set;}
    public Map<String, List<sObject>> objRecordsMap{get;set;}
    
    public FindRelatedData()
    {
        
        objFieldsMap = new Map<String, List<string>>();
        objRecordsMap = new Map<String, List<sObject>>();
        objType = id.getsobjecttype().getDescribe().getName();
        System.debug('object Type:'+objType);
        Schema.DescribeSObjectResult relationType = Schema.getGlobalDescribe().get(objType).getDescribe();
        for(Schema.ChildRelationship childs: relationType.getChildRelationships())
        {
            System.debug('childs:'+childs);
            
            Schema.SObjectType sobj = childs.getChildSObject();
            System.debug('sobjects:'+sobj);          
            childName = String.valueOf(childs.getChildSObject());
            system.debug('Child Object:'+childName);
            List<string> setFlds = new List<String>();
            if(childName == 'SP001_MyDev__Account__c' || childName == 'opportunity' ) 
            {
                Schema.SobjectField fields = childs.getField();
            	fname = childs.getField().getDescribe().getName();
            	System.debug(fname);
                //System.debug('childName:'+childName);
                sObject obj = Schema.getGlobalDescribe().get(childName).newSObject();
                System.debug('objs:'+obj);
                objNames.add(childName);
                childObjs.add(obj);
                fieldlist = new List<string>();
                fmap = childs.getChildSObject().getDescribe().fields.getMap();
                System.debug('Fields*****'+fmap);
                for(String f:fmap.keySet())
                {
                    System.debug('field name:'+f);
                    
                    fieldlist.add(f);
                }
                objFieldsMap.put(childName,fieldlist);
                System.debug('objFields***'+objFieldsMap);
        		objQry = executeqry(fieldlist);
        		System.debug('child objects:'+objQry);
                objRecordsMap.put(childName,queries);
        			System.debug('records:'+objRecordsMap);
        	}
            
        }
    }
    
   
    Public list<sObject> executeqry(List<string> fieldlists)
    {
       		   

               string qry = fieldlists.get(0);
                    for(Integer i =1;i<fieldlists.size();i++)
                    {
                        qry  = qry+ ' , ' + fieldlists.get(i) ;
                        System.debug('Qry----'+qry);
                    }
                     System.debug('printquery:'+' SELECT '+''+ qry +' '+' FROM '+' '+ childName +' '+' WHERE '+''+fname+ ' = \'' +id+'\''+ ' LIMIT '+' '+'3');
                    queries = Database.query(' SELECT '+''+ qry +' '+' FROM '+' '+ childName +' '+' WHERE '+''+fname+ ' = \'' +id+'\''+ ' LIMIT '+' '+'3');
                    System.debug('Query:'+ queries); 
                //executeqry(fieldlist);
            
    				//fieldlist.addAll(fieldlists);
        			
		System.debug('fieldlists:'+fieldlist);
        return queries;
    }
  
}

 
SandhyaSandhya (Salesforce Developers) 
Hi,

When a map contains a entry with a not-null key but a null value and you try to access it in VF page you will receive this error.

Refer below link where you can find workaround.

https://salesforce.stackexchange.com/questions/44828/map-key-null-not-found-in-map-when-using-apexpageblocktable
 
https://success.salesforce.com/issues_view?id=a1p30000000T3k2AAC
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya