KJW
KJW

Reputation: 15251

Java: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

public class MainFrame extends JFrame {
    public DefaultTableModel historyModel;
    public DefaultTableModel dataModel;
    public JTable historyTable;
    public JTable dataTable;
    public JTableHeader header;

private void createUI() {
        browsersTabbedPane = new BrowsersTabbedPane();
        add(browsersTabbedPane.getTabPane(), CENTER);
        addDefaultBrowserComponentForTabbedPane();
        createActions();
        add(createToolBar(), BorderLayout.NORTH);
        add(createStatusBar(), BorderLayout.SOUTH);
        add(createDataView(dataModel,dataTable, header), BorderLayout.PAGE_END);
        add(createHistoryView(historyModel,historyTable), BorderLayout.LINE_END);
        add(createBigButton(browsersTabbedPane.getActiveBrowser(),dataModel,historyModel,dataTable,historyTable), BorderLayout.WEST);

        createMenuBar();
    }

private JPanel createDataView(DefaultTableModel dataModel, JTable dataTable, JTableHeader header){
        JPanel panel = new JPanel();

        Object data[][] = {{"001","vinod","Bihar","India","Biology","65","First"},
                           {"002","Raju","ABC","Kanada","Geography","58","second"},
                           {"003","Aman","Delhi","India","computer","98","Dictontion"},
                           {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
                           {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"}};
        Object col[] = {"Roll","Name","State","country","Math","Marks","Grade"};
        dataModel = new DefaultTableModel(data,col);
        dataTable = new JTable(dataModel);

        dataTable.setPreferredScrollableViewportSize(new Dimension(1500,100));
        header = dataTable.getTableHeader();
        header.setBackground(Color.yellow);
        JScrollPane pane = new JScrollPane(dataTable);
        dataTable.setAutoResizeMode(JTable.WIDTH);
        dataTable.doLayout();
        panel.add(pane);
        return panel;

    }

    private JPanel createBigButton(Browser browser, DefaultTableModel dataModel, DefaultTableModel historyModel, JTable dataTable, JTable historyTable){
        JPanel panel = new JPanel();
        JButton bigbutton = new JButton("Big Button");
        bigbutton.addActionListener(new BigButtonListener(browser,dataModel,historyModel,dataTable, historyTable));
        panel.add(bigbutton);
        return panel;
    }

   class BigButtonListener implements ActionListener{
        Image screenshot;
        Browser bigbrowser;

        BigButtonListener(Browser browser, DefaultTableModel dataModel, DefaultTableModel historyModel, JTable dataTable, JTable historyTable) {
            screenshot = browser.toImage(true);
            bigbrowser = browser;
            historyModel = historyModel;
            dataModel = dataModel;
            dataTable = dataTable;
            historyTable = historyTable;
            //table1.addRow
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // save current image           
            //historyModel.insertRow(2,new Object[]{"Sushil"}); 
            System.out.println(historyTable.getRowCount());

        }   
    }  
}

The problem occurs when the Big button is clicked. It's supposed to output the total row count of the historyTable (which is public).

however this is the stacktrace output

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.my.demo.MainFrame$BigButtonListener.actionPerformed(MainFrame.java:356)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Upvotes: 0

Views: 3951

Answers (2)

Tasman Vose
Tasman Vose

Reputation: 42

You are using the local class variable historyTable to set itself, which is in turn setting itself to null because the original value was null, thus not allowing your desired data to be retrieved

Upvotes: 0

erickson
erickson

Reputation: 269737

You never initialize historyTable to a non-null value.

Upvotes: 2

Related Questions