-2

I am displaying a table in a Java GUI. The user can currently add rows to the table by clicking an 'Add Row' button, and edit the values of the cells in the table. I am now trying to add a method to remove the selected row from the table by clicking a 'Remove Row' button.

I have declared the button as a global variable:

public JButton removeBtn = null;

Then I am adding the listener to the button in my addListeners() method:

private void addListeners(){
    ....
    removeBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            int selectedRow = jEntityFilterTable.getSelectedRow();
            DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();
            model.removeRow(selectedRow);
        }
    });
}

However, when I now try to run my code, I get a NullPointerException that's stopping it from running... The exception says:

Exception in thread "main" java.lang.NullPointerException

The lines it's complaining about are:

removeBtn.addActionListener(new ActionListener(){

(which I guess means it could be anything inside the clock of code above, where I'm adding the ActionListener),

addListeners();

(where I'm calling the addListeners() method), and

JConfigurationPane panel = new JConfigurationPane();

(where I'm initialising the JConfigurationPane in my main() method. Can anyone spot what I'm doing wrong here?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118
  • 3
    `public JButton removeBtn = null;` It's `null`. You must initialize it *before* adding an `ActionListener` to it – BackSlash Jul 07 '14 at 09:09
  • Where do you initialize your button? From the code you posted `removeBtn` is `null`. – Narmer Jul 07 '14 at 09:10
  • What @BackSlash said. Plus: "(which I guess means it could be anything inside the block of code above, where I'm adding the ActionListener)" - no. The stuff in that block is your ActionListener. It runs when an action is performed, not when it's added. – Mark Minto Jul 07 '14 at 09:11
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Jul 07 '14 at 09:13

2 Answers2

1

You have not initialised the removeBtn variable. Replace

public JButton removeBtn = null;

with

public JButton removeBtn = new JButton("Remove"); 

In your current code, on the line

removeBtn.addActionListener(new ActionListener(){

removeBtn does not refer to an object so there is nothing to invoke the addActionListener method on. As a result, you receive a NullPointerException.

JamesB
  • 7,774
  • 2
  • 22
  • 21
  • Thanks for your answer. I've given that a go, and it has cleared up the `NullPointerException` that I was getting, but for some reason, when I click the button, the selected row is not removed from the table. I've added some debug code to the `actionPerformed()` method, but this is never displayed in the console, so it seems that the code inside the `actionPerformed()` method is never called... Any ideas why this is? – Noble-Surfer Jul 07 '14 at 09:33
  • @someone2088 I would raise a separate SO question for the new issue. Make sure you post the relevant code and what you have tried etc. – JamesB Jul 07 '14 at 10:18
0

try to initialize your button like this:

removeBtn = new JButton("Remove");
alex
  • 8,904
  • 6
  • 49
  • 75