javaswingnullpointerexceptionjtabledefaulttablemodel

Adding a row to a table in the GUI


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?


Solution

  • It looks like your button is not instantiated:

    private JButton addBtn = new JButton("Add empty row");
    

    Depending on the IDE you are using, you may enable warnings that could detect such problems.

    Edit:

    model.addRow(new Object[]{"Site", "Application", "Entity"}); should contain the values, or nothing in your case, not the titles.

    I made a quick working example using the syntax you provided as much as I could, if there is something you don't understand feel free to ask.

    public static void main(String[] args)
    {
        JFrame jFrame = new JFrame("Add row");
        jFrame.setSize(new Dimension(1280,720));
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        final String[] columns = new String[]{"C1","C2"};
        String[][] data = new String[][]{new String[]{"A1","A2"},new String[]{"B1","B2"}};
    
        final JTable jTable = new JTable();
        jTable.setModel(new DefaultTableModel(data,columns));
    
        jFrame.add(new JScrollPane(jTable),BorderLayout.CENTER);
    
        JPanel jPanel = new JPanel();
        JButton jButton = new JButton("Add empty row");
        jButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("'Add' button pressed.");
                DefaultTableModel model = (DefaultTableModel)jTable.getModel();
                model.addRow(new String[columns.length]);
            }
        });
    
        jPanel.add(jButton);
        jFrame.add(jPanel,BorderLayout.SOUTH);
        jFrame.setVisible(true);
    }