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
Adelchi PelizzoAdelchi Pelizzo 

I get Null pointer exception Error on VF with Custom SetController extension

public class CaseListCtrl {
    public String s1 {get;set;}
    public String s2 {get;set;}
    public String s3 {get;set;}
    public String s4 {get;set;}
    public String s5 {get;set;}
    public String s6 {get;set;}
    public String s7 {get;set;}
    
    /*List<Case> caseList = [SELECT Id, Labels2__c FROM Case Limit 20];
    
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(caseList);*/
    
    private final Case c;
    
    public CaseListCtrl(ApexPages.StandardSetController controller) {
        this.c = (Case)controller.getRecord();
    }
    
    public List <Case> getCases(){
        return [SELECT Id, Labels2__c FROM  Case];
    }
    
    public List<Case> caseRecords {
        get {
            return [SELECT Id, Labels2__c FROM Case];
        }
        private set;
    }
    
    /*public List<Case> getCaseListCtrl() {
        return (List<Case>) caseRecords.getRecords();
    }*/
    
    Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        String[] setS = new List<String>();
        for (integer i = 0; i < 20; i++){
            Case cR = caseRecords[i];
        	lstS.add(cr.Labels2__c.split(';'));
        }
        if (lstS != null){
            for (integer i = 1; i < lstS.size(); i++){
                s1 = lstS.get(i)[0];
                s2 = lstS.get(i)[1];
                s1 = lstS.get(i)[2];
                s2 = lstS.get(i)[3];
                s1 = lstS.get(i)[4];
                s2 = lstS.get(i)[5];
                s1 = lstS.get(i)[6];
                s2 = lstS.get(i)[7];
            }
        }
		return null;
    }
}
<apex:page standardController="Case" recordSetVar="cases" sidebar="true" standardstylesheets="false" showheader="true" extensions="CaseListCtrl" action="{!getIt}">
<apex:form >
    {!s1}
    {!s2}
    {!s3}
    {!s4}
    {!s5}
    {!s6}
    {!s7}

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

My intent is to grab values from a multi-picklist field "labels2__c" and display it as a single value in a visualforce page.

This is the error: 

 

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!getIt}' in component <apex:page> in page testctrlcase: Class.CaseListCtrl.getIt: line 40, column 1
Class.CaseListCtrl.getIt: line 40, column 1

Thanks,
Adelchi

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
public class CaseListCtrl {
    public String s1 {get;set;}
    public String s2 {get;set;}
    public String s3 {get;set;}
    public String s4 {get;set;}
    public String s5 {get;set;}
    public String s6 {get;set;}
    public String s7 {get;set;}
    
    /*List<Case> caseList = [SELECT Id, Labels2__c FROM Case Limit 20];
    
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(caseList);*/
    
    private final Case c;
    
    public CaseListCtrl(ApexPages.StandardSetController controller) {
        this.c = (Case)controller.getRecord();
    }
    
    public List <Case> getCases(){
        return [SELECT Id, Labels2__c FROM  Case];
    }
    
    public List<Case> caseRecords {
        get {
            return [SELECT Id, Labels2__c FROM Case];
        }
        private set;
    }
    
    /*public List<Case> getCaseListCtrl() {
        return (List<Case>) caseRecords.getRecords();
    }*/
    
    Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        String[] setS = new List<String>();
        for (integer i = 0; i < 20; i++){
            Case cR = caseRecords[i];
			if( cr.Labels2__c != null)
			{
				lstS.add(cr.Labels2__c.split(';'));
			}	
        }
        if (lstS != null){
            for (integer i = 1; i < lstS.size(); i++){
                s1 = lstS.get(i)[0];
                s2 = lstS.get(i)[1];
                s1 = lstS.get(i)[2];
                s2 = lstS.get(i)[3];
                s1 = lstS.get(i)[4];
                s2 = lstS.get(i)[5];
                s1 = lstS.get(i)[6];
                s2 = lstS.get(i)[7];
            }
        }
		return null;
    }
}

 
Nishad KNishad K
Hi Adelchi,

Try like this :
public class CaseListCtrl {
    public String s1 {get;set;}
    public String s2 {get;set;}
    public String s3 {get;set;}
    public String s4 {get;set;}
    public String s5 {get;set;}
    public String s6 {get;set;}
    public String s7 {get;set;}
    
    /*List<Case> caseList = [SELECT Id, Labels2__c FROM Case Limit 20];
    
    ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(caseList);*/
    
    private final Case c;
    
    public CaseListCtrl(ApexPages.StandardSetController controller) {
        this.c = (Case)controller.getRecord();
    }
    
    public List <Case> getCases(){
        return [SELECT Id, Labels2__c FROM  Case];
    }
    
    public List<Case> caseRecords {
        get {
            return [SELECT Id, Labels2__c FROM Case];
        }
        private set;
    }
    
    /*public List<Case> getCaseListCtrl() {
        return (List<Case>) caseRecords.getRecords();
    }*/
    
    Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        String[] setS = new List<String>();
        for (integer i = 0; i < 20; i++){
            Case cR = caseRecords[i];
        	lstS.add(cr.Labels2__c.split(';'));
        }
        if (lstS != null){
          
                s1 = lstS[0][0];
                s2 = lstS[0][1];
               
        
        }
		return null;
    }
}
Please refer the below link :
http://stackoverflow.com/questions/16840732/how-to-write-a-multidimensional-aray-in-apex

Regards,
Adelchi PelizzoAdelchi Pelizzo
Thank for your answers. Unfortuntely I am still stuck:
This method gives me:

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!getIt}' in component <apex:page> in page testctrlcase: Class.CaseListCtrl.getIt: line 32, column 1
Class.CaseListCtrl.getIt: line 32, column 1
 
Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        String[] setS = new List<String>();
        if(!casesList.isEmpty()){
            for (integer i = 0; i < 20; i++){
                Case cR = casesList[i];
                lstS.add(cR.Labels2__c.split(';'));
            }
            if (lstS != null){
                for (integer i = 1; i < lstS.size(); i++){
                    s1 = lstS.get(i)[0];
                    s2 = lstS.get(i)[1];
                    s1 = lstS.get(i)[2];
                    s2 = lstS.get(i)[3];
                    s1 = lstS.get(i)[4];
                    s2 = lstS.get(i)[5];
                    s1 = lstS.get(i)[6];
                    s2 = lstS.get(i)[7];
                }
            }
        }
		return null;
    }

I tried also this just to test the function on one returned record from the query in casesList. I get this error instead:


System.ListException: List index out of bounds: 2
Error is in expression '{!getIt}' in component <apex:page> in page testctrlcase: Class.CaseListCtrl.getIt: line 61, column 1
Class.CaseListCtrl.getIt: line 61, column 1
 
public class CaseListCtrl{
    public String s1 {get;set;}
    public String s2 {get;set;}
    public String s3 {get;set;}
    public String s4 {get;set;}
    public String s5 {get;set;}
    public String s6 {get;set;}
    public String s7 {get;set;}
    private final Case c;
    
    public CaseListCtrl(ApexPages.StandardSetController controller) {
        this.c = (Case)controller.getRecord();
    }
    
    public List <Case> getCases(){
        return [SELECT Id, Labels2__c FROM  Case];
    }
    
    public List<Case> casesList {
        get {
            return [SELECT Id, Labels2__c FROM Case];
        }
        private set;
    }
    
    /*Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        String[] setS = new List<String>();
        if(!casesList.isEmpty()){
            for (integer i = 0; i < 20; i++){
                Case cR = casesList[i];
                lstS.add(cR.Labels2__c.split(';'));
            }
            if (lstS != null){
                for (integer i = 1; i < lstS.size(); i++){
                    s1 = lstS.get(i)[0];
                    s2 = lstS.get(i)[1];
                    s1 = lstS.get(i)[2];
                    s2 = lstS.get(i)[3];
                    s1 = lstS.get(i)[4];
                    s2 = lstS.get(i)[5];
                    s1 = lstS.get(i)[6];
                    s2 = lstS.get(i)[7];
                }
            }
        }
		return null;
    }*/
    
    Public ApexPages.Action getIt(){
        List<String> lS = new List<String>();        
        if (!casesList.isEmpty()){
                if(casesList.get(0).labels2__c.split(';')[0] != null){
                    ls.add(casesList.get(0).labels2__c.split(';')[0]);
                    s1 = ls[0];
                }
                if(casesList.get(0).labels2__c.split(';')[1]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[1]);
                    s2 = ls[1];
                }
                if(casesList.get(0).labels2__c.split(';')[2]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[2]);
                    s3 = ls[2];
                }
                if(casesList.get(0).labels2__c.split(';')[3]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[3]);
                     s4 = ls[3];
                }
                if(casesList.get(0).labels2__c.split(';')[4]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[4]);
                    s5 = ls[4];
                }
                if(casesList.get(0).labels2__c.split(';')[5]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[5]);
                    s6 = ls[5];
                }
                if(casesList.get(0).labels2__c.split(';')[6]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[6]);
                    s7 = ls[6];
                }               
        }
        return null;
    }
}

Thanks for your help.
Adelchi

 
ShotShot

First of all change for loop to:

for (integer i = 0; i < casesList.size(); i++)
ShotShot

Also:

public class CaseListCtrl{
    public String s1 {get;set;}
    public String s2 {get;set;}
    public String s3 {get;set;}
    public String s4 {get;set;}
    public String s5 {get;set;}
    public String s6 {get;set;}
    public String s7 {get;set;}
    private final Case c;
    
    public CaseListCtrl(ApexPages.StandardSetController controller) {
        this.c = (Case)controller.getRecord();
    }
    
    public List <Case> getCases(){
        return [SELECT Id, Labels2__c FROM  Case];
    }
    
    public List<Case> casesList {
        get {
            return [SELECT Id, Labels2__c FROM Case];
        }
        private set;
    }
    
    Public ApexPages.Action getIt(){
        List<List<String>> lstS = new List<List<String>>();
        List<String> setS = new List<String>();

        if(!casesList.isEmpty()){

            for (integer i = 0; i < casesList.size(); i++){
                Case cR = casesList[i];

                if (cR != null && String.isNotBlank(casesList[i].Labels2__c) ) { 
                    lstS.add(cR.Labels2__c.split(';'));
                }

            }

            if (lstS.size() > 0){
                for (integer i = 1; i < lstS.size(); i++){
                    
                    //I dont get this logic
                    //you overwrite strings, you dont know if lstS.get(i) contains values and how many...

                    // s1 = lstS.get(i)[0];
                    // s2 = lstS.get(i)[1];
                    // s1 = lstS.get(i)[2];
                    // s2 = lstS.get(i)[3];
                    // s1 = lstS.get(i)[4];
                    // s2 = lstS.get(i)[5];
                    // s1 = lstS.get(i)[6];
                    // s2 = lstS.get(i)[7];
                }
            }
        }

		return null;
    }
Adelchi PelizzoAdelchi Pelizzo
@Shot

The one method you mention above was suggested to me.

I am trying this one, but it seems that, it does not stop when an if condition is 'false'. For testing I am feeding List<Cases> CasesList ; with only one record as :
public List<Case> casesList {
        get {
            return [SELECT Id, Labels2__c FROM Case Where id='5000P00000Y4XnpQAF'];
        }
        private set;
    }

This record has 3 values selected in the multipicklist. The loop should stop when there is no value coming from the labels2__c (multipicklist), instead it gives:
System.ListException: List index out of bounds: 3
Error is in expression '{!getIt}' in component <apex:page> in page testctrlcase: Class.CaseListCtrl.getIt: line 65, column 1
Class.CaseListCtrl.getIt: line 65, column 1

Which is the line when it checks for the 4th value.
Shouldn't just exist the loop if the condition is false?
Public ApexPages.Action getIt(){
        List<String> lS = new List<String>();        
        if (!casesList.isEmpty()){
                if(casesList.get(0).labels2__c.split(';')[0] != null){
                    ls.add(casesList.get(0).labels2__c.split(';')[0]);
                    s1 = ls[0];
                }
                if(casesList.get(0).labels2__c.split(';')[1]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[1]);
                    s2 = ls[1];
                }
                if(casesList.get(0).labels2__c.split(';')[2]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[2]);
                    s3 = ls[2];
                }
                if(casesList.get(0).labels2__c.split(';')[3]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[3]);
                     s4 = ls[3];
                }
                if(casesList.get(0).labels2__c.split(';')[4]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[4]);
                    s5 = ls[4];
                }
                if(casesList.get(0).labels2__c.split(';')[5]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[5]);
                    s6 = ls[5];
                }
                if(casesList.get(0).labels2__c.split(';')[6]!=null){
                    ls.add(casesList.get(0).labels2__c.split(';')[6]);
                    s7 = ls[6];
                }               
        }
        return null;
    }