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
Jyothsna ReddyJyothsna Reddy 

While Adding values to array,I'm getting this error System.NullPointerException: Attempt to de-reference a null object

Hi
Visualforce Code :
<apex:page controller="StringListEx" showheader="false" >
  <apex:form >
     <apex:pageblock >
        Name <apex:inputtext value="{!firVal}"/>
        code <apex:inputtext value="{!secVal}"/>
        Color <apex:inputtext value="{!thirdVal}"/>
        Class <apex:inputtext value="{!fourthVal}"/>
        <apex:commandButton value="Display" action="{!dislay}"/>
     </apex:pageblock>
  </apex:form>
</apex:page>
Apex/Controller Code :
public with sharing class StringListEx {

    public StringListEx (){
   
    } 
    
    public List<String> strLst;
    public String firVal { get; set; }
    public String secVal { get; set; }
    public String thirdVal { get; set; }
    public String fourthVal { get; set; }
    public PageReference dislay() {
        System.debug('First element value::::::::::::::'+firVal);
        System.debug('Second value::::::::::::::'+secVal);
        System.debug('Third value::::::::::::::::'+thirdVal);
        System.debug('Fourth value:::::::::::::::'+fourthVal); 
        strLst.add(firVal);   
        strLst.add(secVal);   
        strLst.add(thirdVal);   
        strLst.add(fourthVal);
        return null;
    }  
}

While Adding values to array,I'm getting this error System.NullPointerException: Attempt to de-reference a null object  at line 17.
In debugging I am getting the values whatever I entered
.Then why it is showing Nullpointer exception


Best Answer chosen by Jyothsna Reddy
Jim JamJim Jam
At line 7, declare the list as public List<String> strLst = new list<String>();

All Answers

Ramu_SFDCRamu_SFDC
Before Line 17 add an if condition to check if firVal is null something like if(firVal!=null){ strLst.add(firVal);  } do the same for all the other values as well
Jim JamJim Jam
At line 7, declare the list as public List<String> strLst = new list<String>();
This was selected as the best answer
Jyothsna ReddyJyothsna Reddy
Condition works for me thanq .but my doubt is I am getting values right..then why I should write If condition...I didnt get this
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Jyotshana,
Please try the following code.
1. You have to create the instance of the list variable.
2. before add any value to list, you must check the it is null or not.

public with sharing class StringListEx {

    public StringListEx (){
  
    }
   
    public List<String> strLst;
    public String firVal { get; set; }
    public String secVal { get; set; }
    public String thirdVal { get; set; }
    public String fourthVal { get; set; }
    public PageReference dislay() {
        strLst = new List<String>();
        System.debug('First element value::::::::::::::'+firVal);
        System.debug('Second value::::::::::::::'+secVal);
        System.debug('Third value::::::::::::::::'+thirdVal);
        System.debug('Fourth value:::::::::::::::'+fourthVal);
        if(String.isNotBlank(firVal))
          strLst.add(firVal); 
        if(String.isNotBlank(secVal))
           strLst.add(secVal); 
        if(String.isNotBlank(thirdVal))
           strLst.add(thirdVal); 
        if(String.isNotBlank(fourthVal)) 
           strLst.add(fourthVal);
        return null;
    } 
}
Thanks & Regards,
Ranjeet Singh.
Jyothsna ReddyJyothsna Reddy

Thanq for reply .Ranjeet.
But my doubt is I checked in debug statements  I got values .then why  I should check in it is null or blank ?
Jyothsna ReddyJyothsna Reddy

Finally I Came to know one thing,
I need to Intialize list
Without writing if(firVal!=null) OR if(String.isNotBlank(secVal)) also it works .
Thanq very much for replying