我一直試圖讓一個JComboBox更新時,一個新項目添加到後端數據庫。將項目添加到JComboBox從不同的類
在實際的代碼中有一個單獨的類來處理添加對話框,當添加新項目時,它會更新數據庫,然後應該通過調用接受字符串的方法將相同的項目添加到下拉菜單主要的GUI類。 (試圖遵循模型 - 視圖 - 控制器)。
下面是一個觸發錯誤的最小示例,儘管在實際應用程序中,它以靜默方式失敗。
我有一個暗示它與對象的實例。此外,爲了填充列表,我使用addItem()遍歷列表,以確保工作正常,並且ComboBox是可變的。
感謝所有幫助湯姆
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestComboBox extends JPanel implements ActionListener{
JComboBox moduleList = new JComboBox(new DefaultComboBoxModel());
TestComboBox testComboBox;
JFrame frame;
public void actionPerformed(ActionEvent e){
if("additem".equals(e.getActionCommand())){
addItem("Item");
}
if("additemfail".equals(e.getActionCommand())){
testComboBox.addItemFail("Item Fail");
}
}
public void addItem(String item){
moduleList.addItem(item);
}
public void addItemFail(String item){
testComboBox = new TestComboBox();
moduleList.addItem(item);
}
protected JPanel createPanel(){
JPanel panel = new JPanel(false);
String[] getModuleList = {"MODULE 1", "MODULE 2"};
moduleList = new JComboBox(new DefaultComboBoxModel(getModuleList));
panel.add(moduleList);
JButton additem = new JButton("Add Item");
additem.setActionCommand("additem");
additem.addActionListener(this);
panel.add(additem);
JButton additemfail = new JButton("Add Item Fail");
additemfail.setActionCommand("additemfail");
additemfail.addActionListener(this);
panel.add(additemfail);
return panel;
}
public void createAndShowGui(){
testComboBox = new TestComboBox();
frame = new JFrame("JComboTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(testComboBox.createPanel());
frame.setSize(450, 150);
frame.setVisible(true);
}
public static void main(String[] args){
TestComboBox t = new TestComboBox();
t.createAndShowGui();
}
}
異常時引發
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at TestComboBox.actionPerformed(TestComboBox.java:16) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253) at java.awt.Component.processMouseEvent(Component.java:6268) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6033) at java.awt.Container.processEvent(Container.java:2045) at java.awt.Component.dispatchEventImpl(Component.java:4629) at java.awt.Container.dispatchEventImpl(Container.java:2103) at java.awt.Component.dispatchEvent(Component.java:4455) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227) at java.awt.Container.dispatchEventImpl(Container.java:2089) at java.awt.Window.dispatchEventImpl(Window.java:2517) at java.awt.Component.dispatchEvent(Component.java:4455) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649) at java.awt.EventQueue.access$000(EventQueue.java:96) at java.awt.EventQueue$1.run(EventQueue.java:608) at java.awt.EventQueue$1.run(EventQueue.java:606) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116) at java.awt.EventQueue$2.run(EventQueue.java:622) at java.awt.EventQueue$2.run(EventQueue.java:620) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.awt.EventQueue.dispatchEvent(EventQueue.java:619) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
第16行是testComboBox = new TestComboBox(),爲什麼hells在世界上...,你再次重新創建類,從本身 – mKorbel
問題是一個空變量。錯誤消息告訴你哪一行導致問題。因此,查看線上使用的所有變量並確定哪一個爲空。然後,您需要更改代碼以確保在嘗試訪問該變量時該變量不爲空。 – camickr