Java Swing JTable NullPointerException

Java Swing JTable NullPointerException

我有一个简单的 Window 和一个 table。但是如果我向我的模型添加超过 4 个对象,我会得到各种 NullPointerExceptions。 这是模型代码:

public class CarTableModel extends AbstractTableModel {
public LinkedList<Car> cars=new LinkedList<Car>();
@Override
public int getRowCount() {
    return 4;
}

@Override
public int getColumnCount() {
    return cars.size();
}

@Override
public Object getValueAt(int row, int column){
    if((row>cars.size()-1) || (column>3) ){
        return null;
    }
    else {
        Car targetCar=cars.get(row);
        switch (column){
            case 0: {return targetCar.brand;}
            case 1: {return targetCar.year;}
            case 2: { return targetCar.volume;}
            case 3: {return targetCar.maxSpeed;}
            default: {return null; }
        }

    }
}

@Override
public Class<?> getColumnClass(int columnIndex){
    switch (columnIndex){
        case 0: {return String.class;}
        case 1: {return Integer.class;}
        case 2: { return Double.class;}
        case 3: {return Double.class;}
        default: {return null; }
    }
}
@Override
public String getColumnName(int columnIndex){
    switch (columnIndex){
        case 0: {return "Brand";}
        case 1: {return "Year";}
        case 2: { return "Volume";}
        case 3: {return "Max Speed";}
        default: {return null; }
    }
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex){
    return false;
}

public void addRow(Car value){
    cars.add(value);
    }
 }

主线来了Windowclass:

public class MainWindow extends JFrame {
public MainWindow(CarTableModel model){
        JTable table=new JTable(model);
        table.getColumnModel().getColumn(0).setPreferredWidth(27);
        table.getColumnModel().getColumn(1).setPreferredWidth(27);
        JScrollPane scrollPane=new JScrollPane(table);
        getContentPane().add(scrollPane);
}

}

这是我用来测试 MainWindow:

的 MainTest class
public class MainTest {
static CarTableModel model;
public static void main(String[] args) {
    model=new CarTableModel();
    for(int i=0;i<10;i++) {
        model.addRow(new Car("Volvo", 4, 4, 4));;
    }

    MainWindow window=new MainWindow(model);
    window.setSize(300,400);
    window.setVisible(true);
}

}

这里我有 Car class 来存储有关汽车的信息:

public class Car {
String brand;
int year;
double volume;
double maxSpeed;

public Car(String brand, int year, double volume, double maxSpeed){
    this.brand=brand;
    this.year=year;
    this.volume=volume;
    this.maxSpeed=maxSpeed;
    }
}

作为输出,如果我向模型添加超过 4 个值,我会得到一个空白 window 和许多异常。当涉及到 GUI 任务时,很难 google 错误,所以如果有人在这里遇到过同样的问题,对不起,请 post 一个 link。

getColumnClass 中的 return null; 更改为 return Object.class;

@Override
public Class<?> getColumnClass(int columnIndex) {
    switch (columnIndex) {
        case 0: {
            return String.class;
        }
        case 1: {
            return Integer.class;
        }
        case 2: {
            return Double.class;
        }
        case 3: {
            return Double.class;
        }
        default: {
            return Object.class;
        }
    }
}

问题实际上是这两种方法的症状...

    @Override
    public int getRowCount() {
        return 4;
    }

    @Override
    public int getColumnCount() {
        return cars.size();
    }

绕错了,getRowCount应该returncars.size()getColumnCount应该return4

我猜这与它有关:

@Override
public int getRowCount() {
    return 4;
}

不应该 return cars.size() 吗?