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?