I have a simple Window with a table. But if I add more than 4 objects to my model, I get various NullPointerExceptions. Here's the model code:
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);
}
}
Here comes the MainWindow class:
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);
}
}
Here's the MainTest class, that I use to test the MainWindow:
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);
}
}
And here I have the Car class to store information about the cars:
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;
}
}
As an output, if I add more than 4 values to the model, I get a blank window and lots of exceptions. It's really hard to google the mistakes when it comes to GUI tasks, so if anyone has already come across with the same problem here, I'm sorry, please post a link.
Change return null;
in getColumnClass
to 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;
}
}
}
The problem is actually a symptom of these two methods...
@Override
public int getRowCount() {
return 4;
}
@Override
public int getColumnCount() {
return cars.size();
}
which are around the wrong way, getRowCount
should return cars.size()
and getColumnCount
should return 4