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
Sudhir_MeruSudhir_Meru 

System.NullPointerException: Attempt to de-reference a null object

Hi, 

  I get below error and line 97 and line 60 from controller. I am doing pagination using wrapper class Please suggest me how to fix this issue.

Class.CCW_ContactPaginationController.fetch_data: line 97, column 1
Class.CCW_ContactPaginationController.<init>: line 60, column 1


public with sharing class CCW_ContactPaginationController {

  //URL Passing Parameters.
  String PageContractId = ApexPages.currentPage().getParameters().get('ContractId');    
  String PageAccountId  = ApexPages.currentPage().getParameters().get('AccountId');  

  public Asset GetAccount{get;set;}
  public Asset GetContract{get;set;}
  public Asset GetExpireDate{get;set;}
  public Asset GetReseller{get;set;}
  public Asset GetDistributor{get;set;}
  public String IncumbentReseller{get;set;}
  public String Education{get;set;}
  public Asset GetOpportunity{get;set;}
  public Asset GetNewOpportunity{get;set;}
  public String ExpireTerms{get;set;}
  public boolean bundsprt{get;set;}

  //Our collection to class/wrapper objects wrapAsset
  public List<CCWRowItem> wrapAssetList {get; set;}
  public List<Asset> selectedAssets{get;set;}
   /*
    *   item in context from the page
    */
    public String contextItem{get;set;}
    public String contextItem2{get;set;}


    /*
    *   set controller
    */
    private ApexPages.StandardSetController setCon;
   
   
    /*
    *   the contact ids selected by the user
    */
    private Set<Id> selectedContactIds;
    private Set<Id> selectedContactIds2= new set<Id>();
   
   
    /*
    *   constructor
    */
    public CCW_ContactPaginationController ()
    {
      GetAccount = new Asset();    //Get Account Id from Page
      GetContract = new Asset();   //Get Contract Name from Page  
      GetExpireDate = new Asset();   // Date Picker  
      GetReseller = new Asset();
      GetDistributor = new Asset(); 
      GetOpportunity = new Asset();   // Get Opportunity Id
      GetNewOpportunity = new Asset();  // Get New Opportunity Name   
      oppreseller = new Opportunity();  
      fetch_data();       
     }
   
      
   public void fetch_data ()
   {
      
        //init variable
        this.selectedContactIds= new Set<Id>();
        //gather data set
       
         If ( GetAccount.AccountId == NULL || GetContract.Name == NULL )
         {  
           If ( PageContractId <> NULL )
           {
           Contract C;
           C = [SELECT Name FROM Contract WHERE Id = :PageContractId Limit 1];
           this.setCon = new ApexPages.StandardSetController( [SELECT AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate,Bundle_Support__c FROM Asset WHERE last_contract_number__c = :C.Name Order by Service_End_Date_Max__c Desc  Limit 1000] );
                     }
           else If ( PageAccountId <> NULL )
           {
           this.setCon = new ApexPages.StandardSetController( [SELECT AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate,Bundle_Support__c FROM Asset WHERE AccountId = :PageAccountId Order by Service_End_Date_Max__c Desc   Limit 1000] );
                     }  
         }
        
          If ( GetAccount.AccountId <> NULL && GetContract.Name == NULL )
          { 
           this.setCon = new ApexPages.StandardSetController( [SELECT AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate,Bundle_Support__c FROM Asset WHERE AccountId = :GetAccount.AccountId Order by Service_End_Date_Max__c Desc  Limit 1000] );
                    }
          Else If ( GetContract.Name <> NULL && GetAccount.AccountId == NULL )
          {
           this.setCon = new ApexPages.StandardSetController( [SELECT AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate,Bundle_Support__c FROM Asset WHERE last_contract_number__c  = :GetContract.Name Order by Service_End_Date_Max__c Desc  Limit 1000] );
                    } 
         Else If ( GetAccount.AccountId <> NULL && GetContract.Name <> NULL )
          {
         this.setCon = new ApexPages.StandardSetController( [SELECT AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c,InstallDate,Bundle_Support__c FROM Asset WHERE AccountId = :GetAccount.AccountId and last_contract_number__c = :GetContract.Name  Order by Service_End_Date_Max__c Desc  Limit 1000] );
                   }                                    
        this.setCon.setpageNumber(1);
        this.setCon.setPageSize(25);                       
   }  
      
    /*
    *   handle item selected
    */
    public void doSelectItem(){       
        this.selectedContactIds.add(this.contextItem);       
    }
   
    public void doSelectItem2(){       
        this.selectedContactIds2.add(this.contextItem2);       
    }
   
   
    /*
    *   handle item deselected
    */
    public void doDeselectItem(){       
        this.selectedContactIds.remove(this.contextItem);       
    }
   
    public void doDeselectItem2(){       
        this.selectedContactIds2.remove(this.contextItem2);       
    }
   
   
    /*
    *   return count of selected items
    */
    public Integer getSelectedCount(){
       
        return this.selectedContactIds.size();
       
    }
   
   
    /*
    *   advance to next page
    */
    public void doNext(){
       
        if(this.setCon.getHasNext())
            this.setCon.next();

    }
   
   
    /*
    *   advance to previous page
    */
    public void doPrevious(){
       
        if(this.setCon.getHasPrevious())
            this.setCon.previous();
               
    }
   
    //public List<CCWRowItem> rows = new List<CCWRowItem>();
   
    public List<CCWRowItem> rows {get; set;}
    /*
    *   return current page of groups
    */
    public List<CCWRowItem> getContacts(){       
       
        rows = new List<CCWRowItem>();
       
        for(sObject r : this.setCon.getRecords()){
            Asset c = (Asset)r;
           
            CCWRowItem row = new CCWRowItem(c);
            if(this.selectedContactIds.contains(c.Id)){
                row.IsSelected=true;
                if (this.selectedContactIds2.contains(c.Id)){
                    row.BundelSelected=true;
                    bundsprt=true;
                }
                else {
                    row.BundelSelected=false;
                    bundsprt=false;
                }

            }
            else {
                row.IsSelected=false;
            }
            rows.add(row);           
        }
               
        return rows;
       
    }
 
// Store Selected Records to Object/Table
public void processSelected() {
    Integer currentPageNumber = setCon.getPageNumber();
    selectedAssets = new List<Asset>();
    list<Temp_Assets__c> TempAssetList = new list<Temp_Assets__c>(); 
       
       
     setCon.setpageNumber(1);

while(true){
    List<Asset>  TempAssetLists = (List<Asset>)setCon.getRecords();
    for(Asset assetIns : TempAssetLists  ) {
          if(this.selectedContactIds.contains(assetIns.Id)){
             selectedAssets.add(assetIns);
            
            
           }
     }
if(setCon.getHasNext()){
  setCon.next();
  }
  else{
  break;
  }
  
}           
       Account ResellerAct;
       Account DistributorAct; 
       Opportunity Oppt;          
                           
       for(Asset Act : selectedAssets)
            {              
              Temp_Assets__c TempAsset = new Temp_Assets__c();
              TempAsset.Name = 'Sudhir';
              TempAsset.AccountId__c = Act.AccountId;
              TempAsset.Product__c = Act.Product2Id;
              //Added by Unnat
              TempAsset.Product_lookup__c= Act.Product2Id;
              TempAsset.Serial_Number__c = Act.SerialNumber;
              TempAsset.Last_Contract_Number__c = Act.last_contract_number__c;
              TempAsset.Service_Start_Date__c = Act.Service_Start_Date_Min__c;
              //TempAsset.Service_End_Date__c = Act.Service_End_Date_Max__c;
              /***************Added by Unnat********************************************/
              if (Act.Service_End_Date_Max__c != null) {
                  TempAsset.Service_End_Date__c = Act.Service_End_Date_Max__c;
              }
              else {
                   TempAsset.Service_End_Date__c = Act.InstallDate;
              }

              TempAsset.Install_Date__c = Act.InstallDate; 
               If (GetReseller.AccountId <> NULL)
               {     
                ResellerAct = [ SELECT Id FROM Account WHERE ID = :GetReseller.AccountId Limit 1];
                TempAsset.Reseller__c = ResellerAct.Id;              
                }
               If (GetDistributor.AccountId <> NULL )
                {
                DistributorAct = [ SELECT Id FROM Account WHERE ID = :GetDistributor.AccountId Limit 1];
                TempAsset.Distributor__c = DistributorAct.Id;
                }
               If ( GetOpportunity.Opportunity__c <> NULL )
                {
                Oppt = [SELECT Id FROM Opportunity WHERE ID = :GetOpportunity.Opportunity__c Limit 1];
                TempAsset.Existing_Opportunity__c = Oppt.Id;
                }               
              TempAsset.Incumbent_Reseller__c = IncumbentReseller;
              TempAsset.Education__c = Education;
              //TempAsset.Expiry_Date__c = GetExpireDate.Expire_Date__c;
              TempAsset.Expiry_Date__c= maxDate;
             
              TempAsset.New_Opportunity__c = GetNewOpportunity.New_Opportunity__c;
              TempAsset.Expiry_Term__c = ExpireTerms;
             
                /* Insert Bundel Checkbox Value */               
                if (this.selectedContactIds2.contains(Act.Id)){
                    bundsprt=true;
                }
                else {
                    bundsprt=false;
                }
   
               
                TempAsset.Bundle_Support__c = bundsprt;
              System.debug('== Bundle Value is: ' + bundsprt);
             
                             //Add the selected Assets to the List
              TempAssetList.add(TempAsset);
            }
           
        Insert TempAssetList; 
       
        setCon.setpageNumber(currentPageNumber);
       
    } 
   
    /*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
       
        return this.setCon.getHasPrevious();
       
    }
   
   
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){
       
        return this.setCon.getHasNext();
   
    }
   
   
    /*
    *   return page number
    */
    public Integer getPageNumber(){
       
        return this.setCon.getPageNumber();
       
    }
   
   
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
   
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
       
        Decimal pages = totalSize/pageSize;
       
        return (Integer)pages.round(System.RoundingMode.CEILING);
    }
   
   
  
   
    /*
    *   helper class that represents a row
    */
    public with sharing class CCWRowItem{
       
        public Asset tContact{get;set;}
        public Boolean IsSelected{get;set;}
        public Boolean BundelSelected{get;set;}
        
        public CCWRowItem(Asset c){
            this.tContact=c;           
            this.IsSelected=false;
            this.BundelSelected=false;
        }
       
    } 
       
}
Mikola SenykMikola Senyk
Hi, Sudhir_Meru

Your property setCon wasn't properly initialized.
It seems you work with visualforce page without URL parameters ContractId and AccountId.
Add to the page parameters (URL) value for ContractId or AccountId. Otherwise make sure you hold not empty properties GetContract or GetAccount. I mean not empty values for GetAccount.AccountId or GetContract.Name.
Sudhir_MeruSudhir_Meru

Hi Mikola, 

   Thanks for your reply Please suggest me how to change as you suggested can you modify the code will follow the same

Thanks

Sudhir

Ramu_SFDCRamu_SFDC
Before setting the page number check if setcon is not null

if(this.setcon!=null){
this.setcon.setpagenumber(1).......
Sudhir_MeruSudhir_Meru
Hi Ramu,

   As suggest have added the conidtion. Now am getting same error in below function

public Boolean getHasPrevious(){
       
        return this.setCon.getHasPrevious();
       
    }


Can we add condition to this function Please suggest

Thanks
Sudhir
Ramu_SFDCRamu_SFDC
Yes add the condition in all the pagination events like previous, next, last, first etc..,
Sudhir_MeruSudhir_Meru
Hi Ramu,

  I added the condition i get this error 

Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 379 column 9


/*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
        if(this.setcon!=null){
        return this.setCon.getHasPrevious();
        }
    }
Ramu_SFDCRamu_SFDC
Hi Sudhir, Add return null ; or return false; which ever is appropriate in else statement. That should fix the problem

public Boolean getHasPrevious(){
        if(this.setcon!=null){
        return this.setCon.getHasPrevious();
        }
else return null; //or else return false
    }
Sudhir_MeruSudhir_Meru
Hi Ramu, 

  As suggested add condition in below functions. Problem is after i click on next and previous there is no data displyaing to navigate

/*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
        if(this.setcon!=null){
        return this.setCon.getHasPrevious();
        }
        else return null;
    }
   
   
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){
        if(this.setcon!=null){
        return this.setCon.getHasNext();
        }
        else return null;
    }
   
   
    /*
    *   return page number
    */
    public Integer getPageNumber(){
        if(this.setcon!=null){
        return this.setCon.getPageNumber();
        }
        else return null;
    }
   
   
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
        if(this.setcon!=null){
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
       
        Decimal pages = totalSize/pageSize;
       
        return (Integer)pages.round(System.RoundingMode.CEILING);
        }
         else return null;
    }