来自DefaulListModel的NullPointerException

时间:2014-01-17 22:09:37

标签: java swing defaultlistmodel

我正在使用Swing GUI进行评分系统。出于某种原因,我的删除Jbutton以及清除模型的方法在调用时开始返回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)

2 个答案:

答案 0 :(得分:1)

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

简而言之,你有:

DefaultListModel<Student> model;  // model is null

但需要:

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

如果这不能解决问题,那么您的错误位于代码未显示的其他地方,您将需要创建并发布Minimal, Complete, Tested and Readable example

答案 1 :(得分:0)

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