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());
}
}
}
单击大按钮时,会发生此问题。应该输出historyTable(公共的)的总行数。
但这是stacktrace输出
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)
最佳答案
您永远不会将historyTable
初始化为非空值。