I have a simple Java GUI which is displaying a JTable
and a few buttons. I want to add an ActionListener
to one of the buttons, so that every time it's clicked, it will add a new, empty, editable row to the table. I've tried to do this by following the accepted answer to the question at: How to add row in JTable?
I currently have a method which is adding listeners to some of the other elements in my GUI, so I've tried editing that method to include what is suggested by the answer to that question. Having done that, my addListeners()
method now looks like this:
public void addListeners()
// Code to add existing listeners
/*Add listeners to the buttons */
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("'Add' button pressed. ");
DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();
model.addRow(new Object[]{"Site", "Application", "Entity"});
}
});
}
However, when I run my code, and click the button that calls this method, I get an error in the console which says:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
The line it's complaining about is:
addBtn.addActionListener(new ActionListener(){
But I can't see why I'm getting a NullPointerException
... addBtn
and jEntityFilterTable
have been declared as global variables within the class using:
private JButton addBtn;
private JTabel jEntityFilterTable = new JTable(new DefaultTableModel());
Anyone have any suggestions?