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
SFDC Apex DevSFDC Apex Dev 

getting error null pointer exception for this controller class...

APEX CLASS

public with sharing class MultiselectController {
    public SelectOption[] leftOptions { get; set; }
    public SelectOption[] rightOptions { get; set; }

    @TestVisible private void setOptions(SelectOption[] options, String value) {
        system.debug('##--setOptions.options: '+ options);
        system.debug('##--setOptions.value: '+ value);
        options.clear();
        String[] parts = value.split('&');
        for (Integer i=0; i<parts.size()/2; i++) {
            options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
              EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
        }
    }

    public String leftOptionsHidden { get; set {
           leftOptionsHidden = value;
           setOptions(leftOptions, value);
        }
    }

    public String rightOptionsHidden { get; set {
           rightOptionsHidden = value;
           setOptions(rightOptions, value);
        }
    }
}

TEST CLASS
@isTest
public class MultiselectControllerTest {
    
    static testMethod void testOptions(){        
        MultiselectController msc=new MultiselectController();
        SelectOption[] selop = new SelectOption[]{};
        String str='UTF-8';
        msc.setOptions(selop,str);
        selop.clear();
    }
    
    static testMethod void testOptions1(){        
        MultiselectController msc=new MultiselectController();
        List<SelectOption> leftOptions = new List<SelectOption>{};        
        String str1='UTF-8';
        msc.leftOptionsHidden = 'PB';
        msc.setOptions(leftOptions, str1);
    }
    
    static testMethod void testOptions2(){        
        MultiselectController msc=new MultiselectController();
        List<SelectOption> rightOptions = new List<SelectOption>{};        
        String str2='UTF-8';
        msc.rightOptionsHidden = 'Pitney';
        msc.setOptions(rightOptions, str2);
    }
}

getting error: null pointer exception: attempt to de-reference to null object
at bold and italic and underlined line in test class
Raj VakatiRaj Vakati
Here is the class
public with sharing class MultiselectController {
    public SelectOption[] leftOptions { get; set; }
    public SelectOption[] rightOptions { get; set; }
    
    @TestVisible private void setOptions(SelectOption[] options, String value) {
        options = new SelectOption[]{};
        system.debug('##--setOptions.options: '+ options);
        system.debug('##--setOptions.value: '+ value);
        options.clear();
        String[] parts = value.split('&');
        for (Integer i=0; i<parts.size()/2; i++) {
            options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
                                         EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
        }
    }
    
    public String leftOptionsHidden { get; set {
        leftOptionsHidden = value;
        setOptions(leftOptions, value);
    }
                                    }
    
    public String rightOptionsHidden { get; set {
        rightOptionsHidden = value;
        setOptions(rightOptions, value);
    }
                                     }
}

And test class
 
@isTest
public class MultiselectControllerTest {
    
    static testMethod void testOptions(){        
        MultiselectController msc=new MultiselectController();
        SelectOption[] selop = new SelectOption[]{};
            String str='UTF-8&UTF-1&ISO';
        msc.setOptions(selop,str);
        selop.clear();
    }
    
    static testMethod void testOptions1(){        
        MultiselectController msc=new MultiselectController();
        List<SelectOption> leftOptions = new List<SelectOption>{};        
            String str1='UTF-8&UTF-1&ISO';
        msc.leftOptionsHidden = 'PB';
        msc.setOptions(leftOptions, str1);
    }
    
    static testMethod void testOptions2(){        
        MultiselectController msc=new MultiselectController();
        List<SelectOption> rightOptions = new List<SelectOption>{};        
            String str2='UTF-8&UTF-1&ISO';
        msc.rightOptionsHidden = 'Pitney';
        msc.setOptions(rightOptions, str2);
    }
}

 
SFDC Apex DevSFDC Apex Dev
Hi Raj,

I am still getting the same error...
Raj VakatiRaj Vakati
You need to change apex class also .. please modify the apex class as like below 

 
public with sharing class MultiselectController {
    public SelectOption[] leftOptions { get; set; }
    public SelectOption[] rightOptions { get; set; }
    
    @TestVisible private void setOptions(SelectOption[] options, String value) {
        options = new SelectOption[]{};
         system.debug('##--setOptions.options: '+ options);
        system.debug('##--setOptions.value: '+ value);
        options.clear();
        String[] parts = value.split('&');
        for (Integer i=0; i<parts.size()/2; i++) {
            options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
                                         EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
        }
    }
    
    public String leftOptionsHidden { get; set {
        leftOptionsHidden = value;
        setOptions(leftOptions, value);
    }
                                    }
    
    public String rightOptionsHidden { get; set {
        rightOptionsHidden = value;
        setOptions(rightOptions, value);
    }
                                     }
}

 
Raj VakatiRaj Vakati
Does it worked ?
SFDC Apex DevSFDC Apex Dev

Hi Raj,
What is the need of this line in apex class options = new SelectOption[]{};

Can you explain me.....plese....

SFDC Apex DevSFDC Apex Dev
Can we can't solve the error without adding this line in the apex class????
Raj VakatiRaj Vakati
No .. you have to initialize the value before using it .. 


So i initialize the value to solve it

 options = new SelectOption[]{};
Raj VakatiRaj Vakati
Is its address your question? Id you dnt initialize the variable you will get null pointer exception