我正在使用Swing GUI创建Java应用程序。我有一个表,每当选择一行时,我希望根据表中所选行中某个项的值启用或禁用某些按钮。
这是我的事件处理程序类的定义:
public class ViewerTableSelectionHandler implements ListSelectionListener{
MainFrame mainFrame;
public ViewerTableSelectionHandler(MainFrame mainFrame){
this.mainFrame = mainFrame;
}
...
初始化JTable后,选择侦听器设置为:
ViewersTable.getSelectionModel().addListSelectionListener(
new ViewerTableSelectionHandler(this)
);
以下是在我的ViewerTableSelectionHandler类中处理选择事件的方法:
@Override
public void valueChanged(ListSelectionEvent e){
this.mainFrame.setViewerButtonsEnabled(
(!(Boolean)this.mainFrame.getViewersTableModel().getValueAt(e.getFirstIndex(), 1)) // <-- THIS line causes the exception
);
}
上面的代码在MainFrame中调用此方法:
public void setViewerButtonsEnabled(boolean state){
ViewerBanButton.setEnabled(state);
ViewerUnbanButton.setEnabled(state);
ViewerTimeoutButton.setEnabled(state);
ViewerTimeoutSecondsLabel.setEnabled(state);
}
使用此代码,我得到了这个例外:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.karmios.nat.botpanel.ViewerTableSelectionHandler.valueChanged(ViewerTableSelectionHandler.java:26)
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.changeSelection(DefaultListSelectionModel.java:405)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:459)
at javax.swing.JTable.changeSelectionModel(JTable.java:2389)
at javax.swing.JTable.changeSelection(JTable.java:2458)
at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1115)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6522)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4530)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
编辑:对于未提及行号的道歉。我现在在上面的代码中添加了一个注释 - 异常是由我的事件处理程序的valueChanged方法中的一行引起的。