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 

ApexPages.StandardSetController Going to null pointer reference

Hi, 

 I created a controller using wrapper class. Problem I am facing is everytime while doing pagination page ApexPages.StandardSetController 
is going to null pointer refrence. 

Value is getting cleared I think while pagination please suggest me how to fix

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   
      fetch_data();       
     }
   
      
   public void fetch_data ()
   {
      
        //init variable
        this.selectedContactIds= new Set<Id>();
        //gather data set
       
        if ( GetAccount.AccountId == NULL & GetContract.Name == NULL )  
         {  
           if ( PageAccountId <> NULL & PageContractId == NULL )
           {
           this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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] );
           }
           else if ( PageAccountId == NULL & PageContractId <> NULL )
           {
           Contract C;
           C = [SELECT Name FROM Contract WHERE Id = :PageContractId Limit 1];
           this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 ( GetAccount.AccountId <> NULL && GetContract.Name == NULL )
        {
         this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 Id, 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 Id, 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] );
        }           
       else  
        this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 = '0013000000DULXG' Order by Service_End_Date_Max__c Desc Limit 1000] );
                                        
                  
        if(this.setCon != null){                                       
        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.Asset_Id__c = Act.Id;
              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;
    Decimal pageSize;
    Decimal pages;
          totalSize = this.setCon.getResultSize();
          pageSize = this.setCon.getPageSize();     
          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;
        }
       
    }
 
      
    
   
}
Ramu_SFDCRamu_SFDC
Hi Sudhir, Yesterday I believe you have included the if condition to check if the controller is null? Were there any other issues by doing that?
Sudhir_MeruSudhir_Meru

Hi Ramu, 

    Still after addition condition null pointer refrence is happening not sure what is the issue

Thanks

Sudhir

Sudhir_MeruSudhir_Meru
Hi Ramu can we add exception when null pointer refrence occurs
Ramu_SFDCRamu_SFDC
What action is throwing the null pointer exception, also please provide your new code.
Sudhir_MeruSudhir_Meru
It is happening in function getContacts below controller line I have highlighted


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   
      fetch_data();       
     }
   
      
   public void fetch_data ()
   {
      
        //init variable
        this.selectedContactIds= new Set<Id>();
        //gather data set
       
         
if ( GetAccount.AccountId == NULL && GetContract.Name == NULL )  
         {  
           if ( PageAccountId <> NULL && PageContractId == NULL )
           {
           this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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] );
           }
           else if ( PageAccountId == NULL && PageContractId <> NULL )
           {
           Contract C;
           C = [SELECT Name FROM Contract WHERE Id = :PageContractId Limit 1];
           this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 ( GetAccount.AccountId <> NULL && GetContract.Name == NULL )
        {
         this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 Id, 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 Id, 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] );
        }           
  else  
      this.setCon = new ApexPages.StandardSetController( [SELECT Id, 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 = '0013000000DULXG' Order by Service_End_Date_Max__c Desc Limit 1000] );
     
     
     
                                        
                  
        if(this.setCon != null){                                       
        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(){       
       try 
        {
        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;
        }
        catch(Exception e) {
           return null;
             }
       
    }
 
// 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.Asset_Id__c = Act.Id;
              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(){      
       try 
        {
        return this.setCon.getHasPrevious();        
        }
        catch(Exception e) {
           return false;
             }
    }
   
   
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){      
    try 
        {
        return this.setCon.getHasNext();
        }
      catch(Exception e) {
           return false;
             }
    }

   
    /*
    *   return page number
    */
    public Integer getPageNumber(){  
    try 
        {    
        return this.setCon.getPageNumber();       
    }
    catch(Exception e) {
           return 1;
             }
    }

   
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
    Decimal totalSize;
    Decimal pageSize;
    Decimal pages;
    try 
        {
          totalSize = this.setCon.getResultSize();
          pageSize = this.setCon.getPageSize();     
          pages = totalSize/pageSize;
          return (Integer)pages.round(System.RoundingMode.CEILING);     
        }
         catch(Exception e) {
           return 1;
             }
    }


   
    /*
    *   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;
        }
       
    }
 
      
    
   
}
Sudhir_MeruSudhir_Meru
Hi Ramu, 

  I think problem is in this parameter

  String PageContractId = ApexPages.currentPage().getParameters().get('ContractId');    
  String PageAccountId  = ApexPages.currentPage().getParameters().get('AccountId');


If i hard code parameter its working
Ramu_SFDCRamu_SFDC
In that case where ever you are using this parameter set the condition

if(PageContractId!=null){
//do something
}
else{
//do soemthing
}

Do the same ever for second variable PageAccountId That should resolve the issue.