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
Nishant Shrivastava 30Nishant Shrivastava 30 

vf page error : attempt to dereference a null object

vfpage controller


global class Search_Account_Controller {
   public Set<string> accountSet{get;set;}
   public string accName{get;set;}
   public Account accObj{get;set;}
   public List<Opportunity> oppList{get;set;}
   public boolean recTypeCheck{get;set;}
   public List<Service__c> serviceList{get;set;} 
   
    public Search_Account_Controller() {
         recTypeCheck=true;
        accountSet = new Set<String>();
        List<Account> accList = [Select id, Name, Account_Type__c from Account LIMIT 30000];
        if(accList!=null && accList.size()>0){
            for(Account acc :accList){
                accountSet.add(acc.Name);
            }
        }
    }
    
     public void getAccountDetails(){
        oppList = new List<Opportunity>();
        serviceList= new List<Service__c>();
         List<Account> accList= [Select Name, Id, Account_Type__c from Account where Name=:accName LIMIT 1];
         
         if(accList!=null && accList.size()>0)
         accObj=accList[0];
         
        if(accObj.Account_Type__c!='Client')  {
             recTypeCheck=false;
         }
         else{
            oppList=[Select Id, OppNumber__c, FC_Opportunity_Status__c, Name, StageName, CloseDate, CreationDateTime__c from Opportunity where AccountId=:accObj.Id];    
         }
     }
     
     public void getLineItemDetails()  {
         String oppIdval = apexpages.currentpage().getparameters().get('OppId');
         serviceList=[Select Id, Product__c, Produkt_ID__c , Product__r.Name, Name from Service__c where Opportunity__c=:oppIdval];    
         
     }
}
Best Answer chosen by Nishant Shrivastava 30
Vikash GoyalVikash Goyal
Hi Nishant,

'accName' variable is not initialized so in method 'getAccountDetails()' there will be no result for 'accList' i.e. accList will be null. Due to this 'accObj' will also be null and it will throw exception where you are checking 'accObj.Account_Type__c!='Client''.

So in Search_Account_Controller constructor initialize 'accName' & 'accObj' :
public Search_Account_Controller() {
       recTypeCheck=true;
       accountSet = new Set<String>();
       accName = '';
       accObj = new Account();
        List<Account> accList = [Select id, Name, Account_Type__c from Account LIMIT 
                                                30000];
        if(accList!=null && accList.size()>0){
            for(Account acc :accList){
                accountSet.add(acc.Name);
            }
        }
    }

Also update 'getAccountDetails()' method :
public void getAccountDetails(){
        oppList = new List<Opportunity>();
        serviceList= new List<Service__c>();
        List<Account> accList= [Select Name, Id, Account_Type__c from Account where 
                                               Name=:accName LIMIT 1];
         
        if(accList!=null && accList.size()>0){
           accObj=accList[0];
         
           if(accObj.Account_Type__c!='Client')  {
               recTypeCheck=false;
           }
           else{
                oppList=[Select Id, OppNumber__c, FC_Opportunity_Status__c, Name, 
                         StageName, CloseDate, CreationDateTime__c from Opportunity where 
                         AccountId=:accObj.Id];    
            }
        }
}

 

All Answers

Vikash GoyalVikash Goyal
Hi Nishant,

'accName' variable is not initialized so in method 'getAccountDetails()' there will be no result for 'accList' i.e. accList will be null. Due to this 'accObj' will also be null and it will throw exception where you are checking 'accObj.Account_Type__c!='Client''.

So in Search_Account_Controller constructor initialize 'accName' & 'accObj' :
public Search_Account_Controller() {
       recTypeCheck=true;
       accountSet = new Set<String>();
       accName = '';
       accObj = new Account();
        List<Account> accList = [Select id, Name, Account_Type__c from Account LIMIT 
                                                30000];
        if(accList!=null && accList.size()>0){
            for(Account acc :accList){
                accountSet.add(acc.Name);
            }
        }
    }

Also update 'getAccountDetails()' method :
public void getAccountDetails(){
        oppList = new List<Opportunity>();
        serviceList= new List<Service__c>();
        List<Account> accList= [Select Name, Id, Account_Type__c from Account where 
                                               Name=:accName LIMIT 1];
         
        if(accList!=null && accList.size()>0){
           accObj=accList[0];
         
           if(accObj.Account_Type__c!='Client')  {
               recTypeCheck=false;
           }
           else{
                oppList=[Select Id, OppNumber__c, FC_Opportunity_Status__c, Name, 
                         StageName, CloseDate, CreationDateTime__c from Opportunity where 
                         AccountId=:accObj.Id];    
            }
        }
}

 
This was selected as the best answer
Nishant Shrivastava 30Nishant Shrivastava 30
the problem is little bit different. it is not querying for all 50k records. if i set the limit upto 50000 or unlimited. it starts collapsing the page. it is querying for upto 10k records but for more than that like 30k or more. the page starts collapsing. kindly direct me thereto. i have mentioned my both codes vf page and controller above.

<apex:page sidebar="false" controller="Search_Account_Controller" showHeader="false" docType="html-5.0">
  <html xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink">  
    <head>
    <apex:slds />
         <style>
             ::placeholder {
              color: white;
              opacity: 1;
            }
            
            :-ms-input-placeholder { 
             color: white;
            }
            
            ::-ms-input-placeholder {
             color: white;
            }
         </style> 
    </head>
    <body>    
      <apex:form > 
          <apex:actionFunction name="updateLineItem"  action="{!getLineItemDetails}"  rerender="serviceDetails" >
              <apex:param name="OppId" value="" />
          </apex:actionFunction>
                <div class="slds" id="pageBody">
                     <div class="slds-form-element__control" >
                         <apex:inputText html-placeholder="Search for Account(Type Customer)" style="margin-top: 10%;width: 30%;background-color: gray;color:white"  Styleclass="slds-input"  value="{!accName}"   html-autocomplete="on" list="{!accountSet}">
                             <apex:actionSupport event="onchange" action="{!getAccountDetails}" oncomplete="checkAccount()" reRender="accountDetails,opportunityDetails,serviceDetails,accountCheckId"/>
                         </apex:inputText>
                     </div>
                     
                     <apex:outputPanel id="accountDetails">
                         <form >
                             <fieldset style="width: 35%;border: 2px solid black;margin-left: 10px;">
                                <legend style="margin-left: 34px;" >Account Details</legend>
                               
                               <div style="margin-left: 12px;padding:5px"> 
                                <div class="slds-text-body--regular" title="Planning Id"><b>Account Id</b></div>
                                 <div class="slds-form-element__control">
                                    <apex:inputText Styleclass="slds-input" style="width: 90%;background-color:white" value="{!accObj.Id}" disabled="true">
                                    </apex:inputText>
                                </div>
                              </div> 
                               
                              <div style="margin-left: 12px;padding:5px">     
                                <label class="slds-text-body--regular" title="Account Name"><b>Account Name</b></label>
                                <div class="slds-form-element__control">
                                    <apex:inputText Styleclass="slds-input" style="width: 90%;background-color:white" value="{!accObj.Name}" disabled="true">
                                    </apex:inputText>
                                </div>
                            </div>
                            
                            <div style="margin-left: 12px;padding:5px">    
                                <label class="slds-text-body--regular" title="Account Type"><b>Account Type</b></label>
                                <div class="slds-form-element__control" >
                                    <apex:inputText Styleclass="slds-input" style="width: 90%;background-color:white" value="{!accObj.Account_Type__c}" disabled="true">
                                    </apex:inputText>
                                </div>
                            </div>    
                            </fieldset> 
                         </form>
                      </apex:outputPanel>
                      
                      <apex:outputPanel id="opportunityDetails">
                          <form >
                             <fieldset style="border: 2px solid black;width: 59.5%;margin-top: -205px;margin-left: 950px;">
                               <legend style="margin-left: 24px;" >Opportunities</legend>
                               <table>
                                <tr><th>Opp. Nr.</th><th>Opportunity Name</th> <th>Opportunity Case</th><th>Stage</th><th>Creation Date</th><th>Close Date</th></tr>
                                <apex:repeat value="{!oppList}" var="oppObj" >
                                <tr><th>{!oppObj.OppNumber__c}</th><th id="{!oppObj.Id}" onclick="updateLineItem(this.id)"><a href="#">{!oppObj.Name}</a></th> <th>{!oppObj.FC_Opportunity_Status__c}</th><th>{!oppObj.StageName}</th>
                                
                                <th>
                                    <apex:outputText value="{0,date,dd'.'MM'.'yyyy}" >  
                                      <apex:param value="{!oppObj.CreatedDate}"/>  
                                    </apex:outputText>  
                                </th>
                                <th>
                                    <apex:outputText value="{0,date,dd'.'MM'.'yyyy}" >  
                                      <apex:param value="{!oppObj.CloseDate}"/>  
                                    </apex:outputText>  
                                </th>
                                </tr>
                                </apex:repeat>
                               </table>
                             </fieldset>
                             
                         </form>
                      </apex:outputPanel>
                    <div style="margin-top: 158px;">  
                      <apex:outputPanel id="serviceDetails">
                          <form style="width: 93%;    margin-top: 167px;">
                             <fieldset style="border: 2px solid black;    margin-left: 10px;">
                               <legend style="margin-left: 24px;" >Services</legend>
                               
                               <table>
                                <tr><th>Service Id</th><th>Service Name</th> <th>Product Id</th><th>Product Name</th><th>Publisher</th></tr>
                                
                                <apex:repeat value="{!serviceList}" var="serObj" >
                                <tr><th>{!serObj.Id}</th><th >{!serObj.Name}</th> <th>{!serObj.Product__c}</th><th>{!serObj.Product__r.Name}</th><th>{!serObj.Publisher__c}</th>
                                
                                
                                </tr>
                                </apex:repeat>
                               </table>
                             </fieldset>
                         </form>
                      </apex:outputPanel>
                    </div>
                </div>
        </apex:form>
        
        <script>
            function updateVal(Id){
               
                updateLineItem(Id);
            }
        </script>
        <apex:outputPanel id="accountCheckId">
            
            <script>
               function checkAccount(){
                   if('{!recTypeCheck}'=='false'){
                       alert('Selected Account is not Customer Account');
                       }
                   }
            </script>
        </apex:outputPanel>
    </body>
   </html>
Nishant Shrivastava 30Nishant Shrivastava 30
ggr