0
我已經做了JTable
與DefaultTableModel
,我裏面添加數據和一切,但是當我試圖找回我得到NullPointerException
這裏的DefaultTableModel getModel():顯示java.lang.NullPointerException上
DefaultTableModel dm = (DefaultTableModel)finishedSaleTable.getModel();
private DefaultTableModel finishedSaleModel;
private JTable finishedSaleTable;
public SaleUI() {
String finishedSaleColumns [] = {"#", "Customer", "Employee", "Products", "# Products", "Discount", "Price", "Date"};
finishedSaleModel = new DefaultTableModel(null, finishedSaleColumns);
finishedSaleTable = new JTable(finishedSaleModel){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
finishedSaleTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
finishedSaleTable.getColumnModel().getColumn(0).setPreferredWidth(40);
finishedSaleTable.getColumnModel().getColumn(1).setPreferredWidth(100);
finishedSaleTable.getColumnModel().getColumn(2).setPreferredWidth(100);
finishedSaleTable.getColumnModel().getColumn(3).setPreferredWidth(85);
finishedSaleTable.getColumnModel().getColumn(4).setPreferredWidth(90);
finishedSaleTable.getColumnModel().getColumn(6).setPreferredWidth(75);
finishedSaleTable.getColumnModel().getColumn(7).setPreferredWidth(125);
JScrollPane finishedSaleTableScroll = new JScrollPane(finishedSaleTable);
}
private void finishedSaleClearList()
{
DefaultTableModel dm = (DefaultTableModel)finishedSaleTable.getModel();
while(dm.getRowCount() > 0)
dm.removeRow(0);
}
完整的錯誤:
Exception in thread "main" java.lang.NullPointerException
at ui.SalesUI.finishedSaleClearList(SalesUI.java:641) // DefaultTableModel line
at ui.SalesUI.finishedSaleUpdateList(SalesUI.java:674) // where I call the method
at ui.SalesUI.<init>(SalesUI.java:248)
at ui.SalesUI.main(SalesUI.java:752)
請發佈更多的代碼以及用於'NullPointerException'的完整堆棧跟蹤。 – Asaph
聽起來更像'finishedSaleTable'的引用是'null',但沒有示例代碼,不可能確定 – MadProgrammer
我加了代碼 – Nikola