我很困惑,我已经像 20 次一样重新设计了我的表格模型。我开始认为我的问题可能不在我的表格模型或数据源中,而是在我的 MainView 中。我以前从未使用过标签窗格......也许它不喜欢标签?最后提到了我的 mainView 代码。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
我的表模型
public class MyTable implements TableModel{
ArrayList<Inventory> inventoryList;
MyDataSource source = new MyDataSource();
String[] columnNames = {"ID","Product","Price","Instock","Sold"};
public MyTable(){
inventoryList = source.getInventory();
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return null;
}
@Override
public int getColumnCount() {
return 5;
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
@Override
public int getRowCount() {
return inventoryList.size();
}
@Override
public Object getValueAt(int row, int col) {
Inventory inventory = inventoryList.get(row);
switch(col){
case 0: return inventory.getID();
case 1: return inventory.getProductName();
case 2: return inventory.getPrice();
case 3: return inventory.getInstock();
case 4: return inventory.getSold();
}
return null;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
}
我的主要课程
JScrollPane scrollPane = new JScrollPane();
tabbedPane.addTab("Database", null, scrollPane, null);
table = new JTable(tableModel);
scrollPane.setViewportView(table);