Question

I am quite confused, I have reworked my tablemodel like a 20 times. I am starting to think maybe my problem is not in my tablemodel or my datasource but in my MainView. I have never used tabbedpanes before... Maybe it does not like tabs? My mainView code is mentioned last.

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

}

}

My Main Class

        JScrollPane scrollPane = new JScrollPane();
    tabbedPane.addTab("Database", null, scrollPane, null);

    table = new JTable(tableModel);
    scrollPane.setViewportView(table);      
Was it helpful?

Solution

Your error is here

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

You are using default Renderers for table because of this you must to specify you columns classes. But you return null instead of that, try to replace it by String.class and it will be work.

OTHER TIPS

You will want to extend AbstractTableModel, not just implement TableModel, else your model won't have all the necessary wiring to work right including all the fireXXX methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top