0

我正在为评分系统开发 Swing GUI。出于某种原因,我的 deleteJbutton以及清除模型的方法在调用时开始返回 NPE。他们以前工作过,使用调试器我发现索引是正确的,我找不到他们不工作的原因。

现在,我可以添加到模型中,但由于某种原因我无法清除或删除任何元素。

编辑:我刚刚做了一个测试,没有它就可以工作,这段代码似乎不是问题。

DefaultListModel<Student> model; //List model in View class

private void handleDeleteButton(){ //Controller class method to handle delete button
   int indexToDelete = sv.listSelectedIndex(); 
   if(indexToDelete >= 0){
       model.remove(indexToDelete); //remove Student object from DefaultListModel 
   }

}

public int listSelectedIndex(){ //method in the View class to return the selected list index
    return studentList.getMinSelectionIndex();
}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gradingsystem.SView.setFields(SView.java:136)
at gradingsystem.SController.valueChanged(SController.java:181)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(BasicListUI.java:2601)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultListModel.remove(DefaultListModel.java:498)
at gradingsystem.SController.handleDeleteButton(SController.java:63)
at gradingsystem.SController.actionPerformed(SController.java:96)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:713)
at java.awt.EventQueue.access$000(EventQueue.java:104)
at java.awt.EventQueue$3.run(EventQueue.java:672)
at java.awt.EventQueue$3.run(EventQueue.java:670)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:686)
at java.awt.EventQueue$4.run(EventQueue.java:684)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:683)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
4

2 回答 2

1

您似乎有一个简单粗心的 java 错误。您已经声明了 DefaultListModel 变量,但在您发布的代码中从未初始化它。

简而言之,您有:

DefaultListModel<Student> model;  // model is null

但需要:

DefaultListModel<Student> model = new DefaultListModel<Student>(); // model not null

如果这不能解决问题,那么您的错误在于未显示的代码中的其他地方,您需要创建并发布一个最小、完整、经过测试和可读的示例

于 2014-01-17T22:11:50.147 回答
0

也许 DefaultListModel 模型是某种不可变的,所以你不能执行删除操作。检查;)

于 2014-01-17T22:43:25.410 回答