TableModel麻烦

时间:2013-10-30 18:39:57

标签: java swing object nullpointerexception tablemodel

我很困惑,我已经将我的桌面模型重做了20次。我开始认为我的问题可能不在我的tablemodel或我的数据源中,而是在我的MainView中。我之前从未使用过tabbedpanes ...也许它不喜欢标签?我的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)

MyTableModel

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);      

2 个答案:

答案 0 :(得分:4)

您的错误在这里

@Override
public Class<?> getColumnClass(int columnIndex) {
    return null;
}

您正在使用表的默认渲染器,因此必须指定列类。但是,如果您返回 null ,请尝试将其替换为String.class,这样就可以了。

答案 1 :(得分:2)

您将需要扩展AbstractTableModel,而不仅仅是实现TableModel,否则您的模型将无法正常工作,包括所有fireXXX方法。