2013-02-05 156 views
0

我已經做了JTableDefaultTableModel,我裏面添加數據和一切,但是當我試圖找回我得到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) 
+0

請發佈更多的代碼以及用於'NullPointerException'的完整堆棧跟蹤。 – Asaph

+1

聽起來更像'finishedSaleTable'的引用是'null',但沒有示例代碼,不可能確定 – MadProgrammer

+0

我加了代碼 – Nikola

回答

1

它可以是兩件事之一。或者:finishedSaleTable爲空或在getModel()之內。但是像其他評論一樣,沒有其他代碼我們可以告訴你。

相關問題