我使用JTable
制作了DefaultTableModel
,我在里面添加数据和所有内容,但是当我尝试检索它时,我在这里得到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 :(得分:1)
它可以是两件事之一。要么:finishedSaleTable
为空,要么是getModel()
内的某些内容。但是和其他评论一样,如果没有剩下的代码,我们就无法告诉你了。