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
sekhar 131sekhar 131 

Visualforce Error :Attempt to de-reference a null object

Visualforce Error :Attempt to de-reference a null object 

line no in 27 apex page
Can any one help me out with this Error .I'm getting
my Task- I want a customized report on Lead owners and their Activities count.
code 
class
public class ActiveHistoryClass
{
 
    public list<wrapper> wrap{set;get;}
    public set<id> setr1{get;set;}
    public map<string,list<wrapper>> ma {set;get;}
    public AggregateResult[] numTasks{set;get;}
    public List<User> userinfo{get;set;}
    
    public ActiveHistoryClass()
    {
       userinfo=[select id,name from User];
        setr1=new set<Id>();
        for(User usr:userinfo)
        {
           setr1.add(usr.id);
        }
        
       numTasks =[select Owner.name owner,Status sta,Activity_type__c  active,count(id) coun from Task GROUP BY Owner.name,Status,Activity_type__c];   
        wrap = new List<wrapper>();
         System.debug('The Aggregate value is'+numTasks);
        
      for(AggregateResult ar:numTasks)
        {
        // wrap.add(new wrapper(ar.get('owner')),(ar.get('sta')),(ar.get('active')),(ar.get('coun')));
           wrap.add(new wrapper(ar));
           system.debug('wrap'+wrap);
        ma.put((string)ar.get('owner'),wrap); 
           // system.debug('ma'+ar);
           setr1.add(ar.id);
            system.debug('setr1'+ar.Id);
        } 
          System.debug('The map value is'+ma);     
        }
    public class wrapper {
        public integer coun{get;set;}
        public String owner{get;set;}
        public String sta{get;set;}
        public String active{get;set;}
        public string oname{set;get;}
   
    public wrapper(AggregateResult ar)
    {
            owner = (String) ar.get('owner');
            sta = (String) ar.get('sta');
            active = (String) ar.get('active');
            coun=(integer) ar.get('coun');
             this.oname=owner;
        }
    }     
}

vf page
<apex:page controller="ActiveHistoryClass" >
    <style>
table {
    font-family: arial, sans-serif; border-collapse: collapse; width: 100%;padding: 10px;
}

td, th {
    border: 1px solid #dddddd;text-align: left;padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<apex:form >
   <apex:pageBlock >
       <!--- <apex:pageBlockTable>---->
       <body>
                      <table>
                          <thead>
                          <tr>
                          <th>Lead.name</th>
                              <th>Status</th>
                              <th>Ativity Type</th>
                              <th>Count</th>
                          </tr>
                              </thead>
    <apex:repeat value="{!setr1}" var="ownerids">
   
 <!---<tr>
                          <td>{!ownerids.owner}</td>
                               <td>{!ownerids.sta}</td>
                               <td>{!ownerids.active}</td>
                               <td>{!ownerids.coun}</td>
                          </tr> --->                        
        <apex:repeat value="{!ma[ownerids]}" var="c">
            <tbody>
                          <tr>
                          <td>{!c.owner}</td>
                               <td>{!c.sta}</td>
                               <td>{!c.active}</td>
                               <td>{!c.coun}</td>
                          </tr>
                          </tbody>
           
            
            <!---<apex:column value="{!c.o}" headerValue="Lead.name" styleClass="padding: 13px;" style="font-family: arial, sans-serif; border-collapse: collapse; width: 30%;padding: 10px"/> 
                      <apex:column value="{!c.s}"  headerValue="Status"/>
                         <apex:column value="{!c.a}"  headerValue="Ativity Type"/>
                           
                          <apex:column value="{!c.ct}"  headerValue="Count"/>---->
                         </apex:repeat>  
                      
                   
      
        </apex:repeat>
                          
                           </table>    
             </body>
       </apex:pageBlock>
    </apex:form>
</apex:page>

please help me
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sekhar,

Greetings to you!

You need to instantiate ma in the constructor as default value of ma is null. That's why you are getting this error.

Try this:
public class ActiveHistoryClass
{
 
    public list<wrapper> wrap{set;get;}
    public set<id> setr1{get;set;}
    public map<string,list<wrapper>> ma {set;get;}
    public AggregateResult[] numTasks{set;get;}
    public List<User> userinfo{get;set;}
    
    public ActiveHistoryClass()
    {
       ma = new map<string,list<wrapper>>();
       userinfo=[select id,name from User];
        setr1=new set<Id>();
        for(User usr:userinfo)
        {
           setr1.add(usr.id);
        }
        
       numTasks =[select Owner.name owner,Status sta,Activity_type__c  active,count(id) coun from Task GROUP BY Owner.name,Status,Activity_type__c];   
        wrap = new List<wrapper>();
         System.debug('The Aggregate value is'+numTasks);
        
      for(AggregateResult ar:numTasks)
        {
        // wrap.add(new wrapper(ar.get('owner')),(ar.get('sta')),(ar.get('active')),(ar.get('coun')));
           wrap.add(new wrapper(ar));
           system.debug('wrap'+wrap);
        ma.put((string)ar.get('owner'),wrap); 
           // system.debug('ma'+ar);
           setr1.add(ar.id);
            system.debug('setr1'+ar.Id);
        } 
          System.debug('The map value is'+ma);     
        }
    public class wrapper {
        public integer coun{get;set;}
        public String owner{get;set;}
        public String sta{get;set;}
        public String active{get;set;}
        public string oname{set;get;}
   
    public wrapper(AggregateResult ar)
    {
            owner = (String) ar.get('owner');
            sta = (String) ar.get('sta');
            active = (String) ar.get('active');
            coun=(integer) ar.get('coun');
             this.oname=owner;
        }
    }     
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
sekhar 131sekhar 131
Thank you Khan Anas 
sekhar 131sekhar 131
Hi Khan Anas
Another error  in apex page
Map key 0057F000003VsteQAC not found in map
Error is in expression '{!ma[ownerids]}' in component <apex:repeat> in page samplevf
 code
<apex:page controller="ActiveHistoryClass" >
    <style>
table {
    font-family: arial, sans-serif; border-collapse: collapse; width: 100%;padding: 10px;
}

td, th {
    border: 1px solid #dddddd;text-align: left;padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<apex:form >
   <apex:pageBlock >
       <!--- <apex:pageBlockTable>---->
                              <body>
                              <table>
                              <thead>
                              <tr>
                              <th>Lead.name</th>
                              <th>Status</th>
                              <th>Ativity Type</th>
                              <th>Count</th>
                              </tr>
                              </thead>
    <apex:repeat value="{!setr1}" var="ownerids"> 
 <!---<tr>
                          <td>{!ownerids.owner}</td>
                               <td>{!ownerids.sta}</td>
                               <td>{!ownerids.active}</td>
                               <td>{!ownerids.coun}</td>
                          </tr> --->                        
        <apex:repeat value="{!ma[ownerids]}" var="c">
            <tbody>
                          <tr>
                          <td>{!c.owner}</td>
                               <td>{!c.sta}</td>
                               <td>{!c.active}</td>
                               <td>{!c.coun}</td>
                          </tr>
                          </tbody>
           
            <!---<apex:column value="{!c.o}" headerValue="Lead.name" styleClass="padding: 13px;" style="font-family: arial, sans-serif; border-collapse: collapse; width: 30%;padding: 10px"/> 
                      <apex:column value="{!c.s}"  headerValue="Status"/>
                         <apex:column value="{!c.a}"  headerValue="Ativity Type"/>     
                          <apex:column value="{!c.ct}"  headerValue="Count"/>---->
                         </apex:repeat>        
                     </apex:repeat>
                </table>    
             </body>
       </apex:pageBlock>
    </apex:form>
</apex:page>
 
Khan AnasKhan Anas (Salesforce Developers) 
VisualForce throws an error when a key is not found in the map, so you have to take care of knowing whether the key is there or not.


Visualforce:
<apex:pageBlockSection rendered="{!renderTable}">
    ...
</apex:pageBlockSection>

Controller:
public Boolean getRenderTable(){
    return mapToCheck.containsKey('keyToCheck');
}

Refer: https://salesforce.stackexchange.com/questions/15748/can-we-check-if-a-key-exists-in-a-map-in-vf-page

Regards,
Khan Anas
sekhar 131sekhar 131
Thank you Khan Anas Sir