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
PravinPravin 

Map<string,list<product2>> use in visual force page to display records

Hi All,

I am trying to display Product2 record based on product family and on selection of particular family want to prepopulate few fields like product family opportunity etc to a custom object edit detail page. i did some programing but getting the following error

Map key null not found in map
Error is in expression '{!map_productfamily[allpr]}' in component <apex:pageBlockTable> in page qualification

My code
VF
<apex:page id="p" standardcontroller="Qualification__c" extensions="Qualification" standardStylesheets="true" tabStyle="Qualification__c">

  <apex:form id="frm">
   
  <apex:pageblock id="allprods" title="Available Products" rendered="{!bool_nshow}">
               <apex:pageblocktable id="allprods" value="{!map_productfamily}" var="allpr">
                   <!-- <apex:column headervalue="Select Family">                   
                       <apex:actionsupport action="{!selectprodfam}" event="onclick" rerender="allpro,allprods,frm" status="reload"> 
                        <input type="radio" />                   
                            <apex:param name="prodid" value="{!allpro.Id}">
                        </apex:param></apex:actionsupport>                           
                    </apex:column>   -->
                    <apex:repeat value="{!map_productfamily[allpr]}" var="allpro" >
                    <apex:column headervalue="Product Family" rowspan="">
                        <apex:outputfield value="{!allpro.Product_Family__c}">
                    </apex:outputfield></apex:column>  
                   
                    <apex:column headervalue="Segment">
                        <apex:outputfield value="{!allpro.Segment__c}">
                    </apex:outputfield></apex:column> 
                    <apex:column headervalue="Sub Segment">
                        <apex:outputfield value="{!allpro.Sub_Segment__c}">
                    </apex:outputfield></apex:column> 
                   
                    <apex:column headervalue="Active">
                        <apex:outputfield value="{!allpro.IsActive}">
                    </apex:outputfield></apex:column>
                    </apex:repeat>
     
                </apex:pageblocktable>
               
    <apex:actionStatus id="reload" style="align:center;" >
        <apex:facet name="start">
            <apex:image value="/img/loading.gif" title="Processing..."/>
        </apex:facet>
        <apex:facet name="stop"></apex:facet>
    </apex:actionStatus>
                       
</apex:pageblock>

<apex:pageblock id="selectprods" title="Products available Under selected Family" rendered="{!bool_show}">
               <apex:pageblocktable id="allprods" value="{!list_pp}" var="allpro">
                   <!-- <apex:column headervalue="Select">                   
                       <apex:actionsupport action="{!selectprod}" event="onclick" rerender="allpro" status="reload"> 
                        <input type="radio" />                   
                            <apex:param name="prodid" value="{!allpro.Id}">
                        </apex:param></apex:actionsupport>                           
                    </apex:column>   -->
                   
                    <apex:column headervalue="Product Name">
                        <apex:outputfield value="{!allpro.Name}">
                    </apex:outputfield></apex:column>  
               
                    <apex:column headervalue="Segment">
                        <apex:outputfield value="{!allpro.Segment__c}">
                    </apex:outputfield></apex:column> 
                    <apex:column headervalue="Sub Segment">
                        <apex:outputfield value="{!allpro.Sub_Segment__c}">
                    </apex:outputfield></apex:column> 
                   
                    <apex:column headervalue="Active">
                        <apex:outputfield value="{!allpro.IsActive}">
                    </apex:outputfield></apex:column>
                   
    
                </apex:pageblocktable>
            
   
     <apex:actionStatus id="disablebtn">
     <apex:facet name="stop">    
           <apex:commandButton value="Back" onClick="BackActionFunc();" status="disablebtn" disabled="false" reRender="disablebtn"/>  
     </apex:facet>    
     <apex:facet name="start">
      <apex:commandButton value="Processing..." onClick="BackActionFunc();" status="disablebtn" disabled="true" reRender="disablebtn"/>                </apex:facet>
    </apex:actionStatus>
  
   
    <apex:actionStatus id="reload" style="align:center;" >
        <apex:facet name="start">
            <apex:image value="/img/loading.gif" title="Processing..."/>
        </apex:facet>
        <apex:facet name="stop"></apex:facet>
    </apex:actionStatus>
    
       
                     
</apex:pageblock>
<apex:actionFunction name="BackActionFunc" action="{!setBoolean}" rerender="frm,allprods,selectprods" />

</apex:form>
</apex:page>




Apex:

/************************************************************************************
Opportunity Product field mapping during new qualification creation
Created By:Pravin
Created Date:10/06/2014
*************************************************************************************/
global class Qualification{
    private list<OpportunityLineItem> list_opplineitem;
    public list<Product2> list_product{get;set;}
    public boolean bool_show{get;set;}
    public boolean bool_nshow{get;set;}
    public list<Product2> list_pp{get;set;}
    public map<string,list<Product2>> map_productfamily=new map<string,list<Product2>>();
    public map<string,list<Product2>> getmap_productfamily(){
        return map_productfamily;
    }
    string str_oppname;
    Id opidd;
    set<Id>    set_prodid=new set<Id>();
    public Qualification(ApexPages.StandardController controller) {
    bool_show=false;
    bool_nshow=true;
    system.debug('>>contbool_nshow>>>>'+bool_nshow);
    list_opplineitem= [SELECT Id,Product2Id FROM OpportunityLineItem
                   WHERE OpportunityId = :ApexPages.currentPage().getParameters().get('oppid')];
                   system.debug('????'+list_opplineitem);
                  
    str_oppname=[select id,name from opportunity where id  = :ApexPages.currentPage().getParameters().get('oppid')].Name;           
    opidd= ApexPages.currentPage().getParameters().get('oppid');             
   
    /*******************************************************************/
        for(OpportunityLineItem ol: list_opplineitem){
            set_prodid.add(ol.Product2Id);
        }
        list_product=[select Id,Name,Product2.Family,Product_Family__r.Name,Product_Family__c,IsActive,Segment__c,Sub_Segment__c from Product2 where id IN:set_prodid/* order by Product2.Family ASC*/];
        system.debug('>>>'+list_product);
        for(product2 pp:list_product){
        if(pp.Product_Family__r.Name!=null && pp.Product_Family__r.Name!=''){
            if(map_productfamily.containskey((string)pp.Product_Family__r.Name)){
                list<Product2> temp_p=map_productfamily.get(pp.Product_Family__r.Name);
                temp_p.add(pp);
                map_productfamily.remove(pp.Product_Family__r.Name);
                map_productfamily.put((string)pp.Product_Family__r.Name,temp_p);
            }
            else{
               
                map_productfamily.put((string)pp.Product_Family__r.Name,new list<product2>{pp});
            }
        }
       
        }
    
    /*********************************************************************/
   
    //if(list_opplineitem.size()>0)
    //findproductfamily(list_opplineitem);
   
    }
   
    public void findproductfamily(list<OpportunityLineItem> list_litem){
       
        for(OpportunityLineItem ol: list_litem){
            set_prodid.add(ol.Product2Id);
        }
        list_product=[select Id,Name,Product2.Family,Product_Family__c,IsActive,Segment__c,Sub_Segment__c from Product2 where id IN:set_prodid/* order by Product2.Family ASC*/];
        system.debug('>>>'+list_product);
       
    }

   Public pagereference selectprod(){
        system.debug('>>>>>>');
        string fmlyname;
        string segname;
        string subsegname;
        Id fmlyid;
        Id segid;
        Id subsegid;
        String selproductid = System.currentPagereference().getParameters().get('prodid');
        system.debug('>>>>>>'+selproductid);
        list<Product2> pp=[select Product2.Family,Product_Family__c,Product_Family__r.Name,Sub_Segment__c,Sub_Segment__r.Name,Segment__c,Segment__r.Name,IsActive from Product2 where Id=:selproductid];
        if(PP.size()>0){
            fmlyid=pp[0].Product_Family__c;
            fmlyname=pp[0].Product_Family__r.Name;
            segid=pp[0].Segment__c;
            segname=pp[0].segment__r.Name;
            subsegid=pp[0].Sub_Segment__c;
            subsegname=pp[0].sub_segment__r.Name;
        }
        PageReference newocp = new PageReference('/a0X/e?CF'+system.Label.Id_of_Opportunity_field_on_Qualification+'='+str_oppname+'&CF'+system.Label.Id_of_Opportunity_field_on_Qualification+'_lkid='+opidd+'&CF'+system.Label.Id_of_Product_Family_field_on_Qualification+'='+fmlyname+'&CF'+system.Label.Id_of_Product_Family_field_on_Qualification+'_lkid='+fmlyid+'&CF'+system.Label.Id_of_Segment_field_on_Qualification+'='+segname+'&CF'+system.Label.Id_of_Segment_field_on_Qualification+'_lkid='+segid+'&CF'+system.Label.Id_of_Sub_Segment_field_on_Qualification+'='+subsegname+'&CF'+system.Label.Id_of_Sub_Segment_field_on_Qualification+'_lkid='+subsegid+'&retURL=/apex/Qualification?oppid='+ApexPages.currentPage().getParameters().get('oppid')+'');
        newocp.setRedirect(true);
        return newocp;
   }
  
    Public void selectprodfam(){
       
        bool_show=true;
        bool_nshow=false;
       
        system.debug('>>fambool_nshow>>>>'+bool_nshow);
        String selproductid = System.currentPagereference().getParameters().get('prodid');
        Product2 pp=[select Product2.Family,Product_Family__c,Product_Family__r.Name,Sub_Segment__c,Sub_Segment__r.Name,Segment__c,Segment__r.Name from Product2 where Id=:selproductid limit 1];
       list_pp=[select Name,Product2.Family,Product_Family__c,Product_Family__r.Name,Sub_Segment__c,Sub_Segment__r.Name,Segment__c,Segment__r.Name,IsActive from Product2 where Product_Family__c=:pp.Product_Family__c And id IN:set_prodid];
      
   }
   
   public Pagereference setBoolean(){
       system.debug('>>setbool_nshow>>>>'+bool_nshow);
       bool_nshow=true;
        bool_show=false;
        PageReference returl = new PageReference('/apex/Qualification?oppid='+opidd);
        returl.setRedirect(true);
        return returl;
   }           
 
  webservice static string findLineItem(Id oppid){
  list<Opportunitylineitem> list_opplineitem=[select id from Opportunitylineitem where OpportunityId =: oppid];
  system.debug('>>>>>>'+list_opplineitem);
  if(list_opplineitem.size()>0){
  return 'true';
  }
  else
  return 'false';
 
  }
 
 

}

looking for a quick reply on this.
Shashikant SharmaShashikant Sharma
First you could remove this statement :

map_productfamily.remove(pp.Product_Family__r.Name);

when you put any value to map it overwrites the value for key. 

To fix the issue try this  :

map_productfamily.put((string)pp.Product_Family__r.Name,temp_p);

put a null check over it that :

if( (string)pp.Product_Family__r.Name != null ) {
map_productfamily.put((string)pp.Product_Family__r.Name,temp_p);
}

put this check to all places where you are filling the map.