-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
5.0, 6
-
Fix Understood
-
generic
-
generic
Name: dmR10075 Date: 08/22/2003
Compile and run the test case below. It exits with NPE in getRowCount.
Exception in thread "main" java.lang.NullPointerException
at TestTableModel.getRowCount(TestTableModel.java:29)
at
javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:194)
at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:98)
at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:116)
at TestTableModel.<init>(TestTableModel.java:9)
at TestTableModel.main(TestTableModel.java:46)
This happens because DefaultTableMode.setDataVector calls getRowCount
before TestTableModel has initialized its data in constructor. This
makes getRowCount unusable when overriding DefaultTableModel. Instead,
setDataVector should have used the value passed into constructor to
determine the amount of rows during creation. See comments for the diffs
for suggested change in DefaultTableModel.
Test:
import javax.swing.*;
import javax.swing.table.*;
public class TestTableModel extends DefaultTableModel {
private String[] names;
private String[] values;
public TestTableModel(String[] names, String[] values) {
super(new String[]{"Field", "Value"}, names.length);
this.names = names;
this.values = values;
}
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
switch(column) {
case 0:
return "Field";
case 1:
return "Value";
}
throw new IllegalArgumentException("there is no such column: " +
column);
}
public int getRowCount() {
return names.length;
}
public Object getValueAt(int row, int col) {
switch(col) {
case 0:
return names[row];
case 1:
return values[row];
}
throw new IllegalArgumentException("there is no such column: " +
col);
}
public boolean isCellEditable(int row, int col) {
return false;
}
public static void main(String[] args) {
JTable table = new JTable(new TestTableModel(new String[]
{"field1"}, new String[] {"value1"}));
}
}
======================================================================
Compile and run the test case below. It exits with NPE in getRowCount.
Exception in thread "main" java.lang.NullPointerException
at TestTableModel.getRowCount(TestTableModel.java:29)
at
javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:194)
at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:98)
at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:116)
at TestTableModel.<init>(TestTableModel.java:9)
at TestTableModel.main(TestTableModel.java:46)
This happens because DefaultTableMode.setDataVector calls getRowCount
before TestTableModel has initialized its data in constructor. This
makes getRowCount unusable when overriding DefaultTableModel. Instead,
setDataVector should have used the value passed into constructor to
determine the amount of rows during creation. See comments for the diffs
for suggested change in DefaultTableModel.
Test:
import javax.swing.*;
import javax.swing.table.*;
public class TestTableModel extends DefaultTableModel {
private String[] names;
private String[] values;
public TestTableModel(String[] names, String[] values) {
super(new String[]{"Field", "Value"}, names.length);
this.names = names;
this.values = values;
}
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
switch(column) {
case 0:
return "Field";
case 1:
return "Value";
}
throw new IllegalArgumentException("there is no such column: " +
column);
}
public int getRowCount() {
return names.length;
}
public Object getValueAt(int row, int col) {
switch(col) {
case 0:
return names[row];
case 1:
return values[row];
}
throw new IllegalArgumentException("there is no such column: " +
col);
}
public boolean isCellEditable(int row, int col) {
return false;
}
public static void main(String[] args) {
JTable table = new JTable(new TestTableModel(new String[]
{"field1"}, new String[] {"value1"}));
}
}
======================================================================