I am creating a Java application with a Swing GUI. I have a table, and whenever a row is selected, I want some buttons to be enabled or disabled based on the value of one of the items in the selected row in the table.
This is the definition of my event handler class:
public class ViewerTableSelectionHandler implements ListSelectionListener{
MainFrame mainFrame;
public ViewerTableSelectionHandler(MainFrame mainFrame){
this.mainFrame = mainFrame;
}
...
And after initialising the JTable, the selection listener is set:
ViewersTable.getSelectionModel().addListSelectionListener(
new ViewerTableSelectionHandler(this)
);
Here is the method that handled the selection event, inside my ViewerTableSelectionHandler class:
@Override
public void valueChanged(ListSelectionEvent e){
this.mainFrame.setViewerButtonsEnabled(
(!(Boolean)this.mainFrame.getViewersTableModel().getValueAt(e.getFirstIndex(), 1)) // <-- THIS line causes the exception
);
}
The above code calls this method in MainFrame:
public void setViewerButtonsEnabled(boolean state){
ViewerBanButton.setEnabled(state);
ViewerUnbanButton.setEnabled(state);
ViewerTimeoutButton.setEnabled(state);
ViewerTimeoutSecondsLabel.setEnabled(state);
}
With this code, I get this exception:
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)
EDIT: Apologies for not mentioning the line number. I have now added a comment in the above code - the exception is caused by a line in the valueChanged method of my event handler.