0

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.

Nat Karmios
  • 325
  • 1
  • 4
  • 17
  • 1
    If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately I don't see that you've told us which line causes the problem. Please fix this so that we can help you. e.g., which line is line 25 in ViewerTableSelectionHandler.java – Hovercraft Full Of Eels Aug 16 '15 at 17:17
  • @HovercraftFullOfEels: It's the `valueChanged` method, which contains *several* spots where an NPE could occur. – Makoto Aug 16 '15 at 17:18
  • @Makoto: thanks. Time to "un-chain" that method call. – Hovercraft Full Of Eels Aug 16 '15 at 17:19
  • Thanks for the update. Now unchain that offending line. Most importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Aug 16 '15 at 17:23
  • @HovercraftFullOfEels, I have edited the answer, indicating the line causing the error. Apologies, I forgot about that. – Nat Karmios Aug 16 '15 at 17:23
  • No problem, but understand that this problem is one you're going to have to solve yourself. Please see my comment above, and the linked-to [duplicate question](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). – Hovercraft Full Of Eels Aug 16 '15 at 17:24
  • Thank you for your help. Time for a good old-fashioned puzzle. :) – Nat Karmios Aug 16 '15 at 17:26
  • Right, by separating out my code a little, I've deduced that the error is being thrown by getValueAt. Anyone have an idea what's happening there? Edit: would this be worth moving to a new question, now that I know the offending method call? – Nat Karmios Aug 16 '15 at 17:46
  • The problem really then is this: why is `getViewersTableModel()` returning a null value. That's where I'd look, and no, I wouldn't ask a new question as you've more debugging still to do. – Hovercraft Full Of Eels Aug 16 '15 at 18:23

0 Answers0