2013-02-11 93 views
-3

我試圖更新從jComboBox1其他Jcomboboxes(帶ItemStateChanged事件)。 我很難知道爲什麼下面的代碼拋出空指針異常。 (jComboBox5已通過jComboBox1更新)當我嘗試從jComboBox1中選擇一個元素,我得到NPE。的JComboBox NullPointerException異常錯誤

private void jComboBox3ItemStateChanged(java.awt.event.ItemEvent evt) {            
     // TODO add your handling code here: 
     try { 
      System.out.println("Count" + jComboBox3.getItemCount()); 
      if(jComboBox3==null) 
      { 
       System.out.println("xczxzvzvvxzxzxc999"); 
      } 
      if (jComboBox3.getItemCount() > 1) { 
       System.out.println("Selected 3 element"+jComboBox3.getSelectedItem()); 
    /*Line 1136*/   if (jComboBox3.getSelectedItem().toString().equals("New Payment Term") == true) { 
        jComboBox3.setSelectedIndex(0); 
        ims.MainWindow.jDesktopPane1.add(new ims.Payment.PaymentTerms("Cust"), javax.swing.JLayeredPane.DEFAULT_LAYER); 
       } 
      } 
     } catch (Exception x) { 
      x.printStackTrace(); 
     } 
    }   

下面是堆棧跟蹤

java.lang.NullPointerException 
    at ims.Customer.CustomerFrame.jComboBox3ItemStateChanged(CustomerFrame.java:1136) 
    at ims.Customer.CustomerFrame.access$100(CustomerFrame.java:22) 
    at ims.Customer.CustomerFrame$2.itemStateChanged(CustomerFrame.java:387) 
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1225) 
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1273) 
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329) 
+0

看起來你不小心把你的整個問題的代碼塊,而不是僅僅的代碼。 – 2013-02-11 17:28:09

+0

它看起來像JComboBox3.getSelectedItem()返回一個空 – 2013-02-11 17:32:07

+1

無需添加'==真'的條件結束:更換'jComboBox3.getSelectedItem()的toString()等於(「新付款期限」)。 =='true' by'jComboBox3.getSelectedItem()。toString()。equals(「New Payment Term」)' – 2013-02-11 18:25:26

回答

1

要查找java.lang.NullPointerException的原因把一個斷點異常在此情況下,1136發生了線,並調查該行的變量。

我的猜測是jComboBox3.getSelectedItem()正在恢復null

相關問題