-1

I am trying to create a list and add values to it but getting nullpointer exception. Can you please assist. Below is my Java code:

acctTypeCodes = new ArrayList();
cumBalIndicators.add(new SelectItem("Y", "Y"));
cumBalIndicators.add(new SelectItem("N", "N"));

Below is the error from log:

java.lang.NullPointerException
    at com.jet.bean.GlAcctRefMBean.getCumBalIndicators(GlAcctRefMBean.java:185)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
shilpa_M
  • 13
  • 9
  • Are you sure that the variable cumBalIndicators is initialized? – Dilshod Shaymanov Mar 07 '17 at 20:45
  • Thank you so much. Can you please help on this http://stackoverflow.com/questions/42638917/how-to-submit-null-value-in-pselectone-primefaces – shilpa_M Mar 07 '17 at 21:17
  • If you have a new question, do not chameleonize an existing and already answered question. Instead, ask a new question. I rolled back your initial question. – BalusC Mar 08 '17 at 08:12

1 Answers1

0

cumBalIndicators is not initialized. If it is a class property its default value is null.
You need to initialize it as follow:

private List<SelectItem> cumBalIndicators = new ArrayList<>(); // or any other implementation of the List interface
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • Thank you so much. The error is resolved. I have another question I changed the list as below but when we chose select as the drop down value it doesnt save null to the database. Can you please let me know public List getCumBalIndicators() { cumBalIndicators = new ArrayList(); cumBalIndicators.add(new SelectItem("", "Select")); cumBalIndicators.add(new SelectItem("Y", "Y")); cumBalIndicators.add(new SelectItem("N", "N")); return cumBalIndicators ; } – shilpa_M Mar 07 '17 at 21:15
  • Below is my xhtml: – shilpa_M Mar 07 '17 at 21:16
  • Hi @shilpa_M can you post the code in the question? Here in the comments is a mess to read – LppEdd Mar 07 '17 at 21:20
  • Thank you so much for the response. I updated the description. I am using jsf selectone menu to display drop down values , when I chose select as the drop down value it should be updated to null but it shows Y/N whichever the user might have previously chosen as dropdown. – shilpa_M Mar 07 '17 at 21:25
  • I'm not an expert on JSF, but usually List is not a good choice for fetching/updating values, as it has no key-value mapping (like Map) – LppEdd Mar 07 '17 at 21:47
  • I am able to update Y/N but only when I chose select it doesn't update it to null – shilpa_M Mar 07 '17 at 21:48
  • On SelectItem have you implemented the toString() method? – LppEdd Mar 07 '17 at 21:52
  • nope. We need to implement this within a managed Bean? Any pseudo code or reference? – shilpa_M Mar 07 '17 at 21:53
  • Try reading this question http://stackoverflow.com/questions/6848970/how-to-populate-options-of-hselectonemenu-from-database – LppEdd Mar 07 '17 at 21:54
  • Thank you for the link. I am not reading the drop down list from the database instead creating it in the bean with Y/N/select values. – shilpa_M Mar 07 '17 at 22:03