2017-03-07 126 views
-1

我想创建一个列表并为其添加值,但获得了空指针异常。你能协助吗? 下面是我的Java代码:如何使用Java将值添加到列表中

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

下面是从日志中的错误:

java.lang.NullPointerException 
    at com.jet.bean.GlAcctRefMBean.getCumBalIndicators(GlAcctRefMBean.java:185) 
+0

你确定变量cumBalIndicators被初始化? –

+0

非常感谢。你可以请帮助这个http://stackoverflow.com/questions/42638917/how-to-submit-null-value-in-pselectone-primefaces –

+0

如果你有一个新的问题,不要chameleonize现有的和已经回答的问题。相反,问一个新问题。我回滚了你最初的问题。 – BalusC

回答

0

cumBalIndicators未初始化。如果它是类属性,则其默认值为null
你需要将其初始化如下:

private List<SelectItem> cumBalIndicators = new ArrayList<>(); // or any other implementation of the List interface 
+0

非常感谢。错误已解决。我有另一个问题,我改变了下面的列表,但是当我们选择select作为下拉值时,它不会将null保存到数据库中。可否请您让我知道,公开名单 getCumBalIndicators() \t \t {\t \t \t cumBalIndicators =新的ArrayList (); (New SelectItem(“”,“Select”)); \t \t cumBalIndicators.add(new SelectItem(“Y”,“Y”)); (新的SelectItem(“N”,“N”)); \t \t return cumBalIndicators; \t \t} –

+0

下面是我的XHTML:\t \t \t \t \t \t \t的 \t \t \t \t \t \t \t的 \t \t \t \t \t \t \t \t \t \t \t –

+0

嗨@shilpa_M您可以张贴在问题的代码?在这里的评论是一团糟阅读 – LppEdd