2017-03-07 127 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