Hi again.
I have am having a problem extending on a DefaultTableModel class. The following is the code that i am working with:
The nex two lines is how i am calling the MyTableModel Class which is an inner class.
MyTableModel mtm = new MyTableModel();
JTable table = new JTable(mtm);
The following class is an inner class:
class MyTableModel extends DefaultTableModel{
Vector vColumnNames;
Vector vData;
public MyTableModel(){
vColumnNames = new Vector();
vData = new Vector();
vColumnNames.addElement("Product");
vColumnNames.addElement("Quantity");
vColumnNames.addElement("Total");
vData.addElement("Hello1");
vData.addElement("Hello2");
vData.addElement("Hello3");
}
public int getRowCount() {
return vData.size(); //The Error occurs here
}
public int getColumnCount() {
return vColumnNames.size();
}
public String getColumnName(int col) {
return (String)vColumnNames.elementAt(col);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return vData.elementAt(rowIndex);
}
public boolean isCellEditable(int row, int col) {
if (row == 0 && col == 0 || row == 0 && col == 1) {
return true;
}if(row == 0 && col == 2){
return false;
}else {
return false;
}
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
vData.setElementAt(value, row);
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
//System.out.print(" " + oData[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
What happens is that when i run the class the following error is thrown:
init:
deps-jar:
compile:
Exception in thread "main" java.lang.NullPointerException
at ordermanagementsystem.OrderForm$MyTableModel.getRowCount(OrderForm.java:156)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:198)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:98)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:80)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:60)
at ordermanagementsystem.OrderForm$MyTableModel.<init>(OrderForm.java:139)
at ordermanagementsystem.OrderForm.<init>(OrderForm.java:76)
at ordermanagementsystem.OrderForm.main(OrderForm.java:131)
Java Result: 1
debug-stepinto:
BUILD SUCCESSFUL (total time: 25 seconds)
The error is on line 20 of the MyTableModel Class, which says
"return vData.size()"
When i run the programe in step through mode and the compiler comes to the line at which i am declaring the object of the MyTableModel Class
MyTableModel mtm = new MyTableModel();
The compiler is then taken to the constructor of the MyTableModel class but only the to the first line
public MyTableModel(){ //Only this line
vColumnNames = new Vector();
vData = new Vector();
vColumnNames.addElement("Product");
vColumnNames.addElement("Quantity");
vColumnNames.addElement("Total");
vData.addElement("Hello1");
vData.addElement("Hello2");
vData.addElement("Hello3");
}
, then the compiler goes straight to the following method:
public int getRowCount() {
return vData.size();
}
Thus the the vectors where not initialised or even known and thus giving the NullPointerException. This is a really odd problem because when i change it to "extends AbstractTableModel" the lines where i declare the variables "vData" and "vColumnNames" then the constructor of the MyTableModel class are before any of the other methods.
Please can anyone help me for the solution. Thanks in advance for any help that anyone gives. Also sorry for posting so much text but i wanted to put as much information as possible about the problem as i could.
Thanks again
Talhah Mafawalla
I have am having a problem extending on a DefaultTableModel class. The following is the code that i am working with:
The nex two lines is how i am calling the MyTableModel Class which is an inner class.
MyTableModel mtm = new MyTableModel();
JTable table = new JTable(mtm);
The following class is an inner class:
class MyTableModel extends DefaultTableModel{
Vector vColumnNames;
Vector vData;
public MyTableModel(){
vColumnNames = new Vector();
vData = new Vector();
vColumnNames.addElement("Product");
vColumnNames.addElement("Quantity");
vColumnNames.addElement("Total");
vData.addElement("Hello1");
vData.addElement("Hello2");
vData.addElement("Hello3");
}
public int getRowCount() {
return vData.size(); //The Error occurs here
}
public int getColumnCount() {
return vColumnNames.size();
}
public String getColumnName(int col) {
return (String)vColumnNames.elementAt(col);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return vData.elementAt(rowIndex);
}
public boolean isCellEditable(int row, int col) {
if (row == 0 && col == 0 || row == 0 && col == 1) {
return true;
}if(row == 0 && col == 2){
return false;
}else {
return false;
}
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
vData.setElementAt(value, row);
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
//System.out.print(" " + oData[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
What happens is that when i run the class the following error is thrown:
init:
deps-jar:
compile:
Exception in thread "main" java.lang.NullPointerException
at ordermanagementsystem.OrderForm$MyTableModel.getRowCount(OrderForm.java:156)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:198)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:98)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:80)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:60)
at ordermanagementsystem.OrderForm$MyTableModel.<init>(OrderForm.java:139)
at ordermanagementsystem.OrderForm.<init>(OrderForm.java:76)
at ordermanagementsystem.OrderForm.main(OrderForm.java:131)
Java Result: 1
debug-stepinto:
BUILD SUCCESSFUL (total time: 25 seconds)
The error is on line 20 of the MyTableModel Class, which says
"return vData.size()"
When i run the programe in step through mode and the compiler comes to the line at which i am declaring the object of the MyTableModel Class
MyTableModel mtm = new MyTableModel();
The compiler is then taken to the constructor of the MyTableModel class but only the to the first line
public MyTableModel(){ //Only this line
vColumnNames = new Vector();
vData = new Vector();
vColumnNames.addElement("Product");
vColumnNames.addElement("Quantity");
vColumnNames.addElement("Total");
vData.addElement("Hello1");
vData.addElement("Hello2");
vData.addElement("Hello3");
}
, then the compiler goes straight to the following method:
public int getRowCount() {
return vData.size();
}
Thus the the vectors where not initialised or even known and thus giving the NullPointerException. This is a really odd problem because when i change it to "extends AbstractTableModel" the lines where i declare the variables "vData" and "vColumnNames" then the constructor of the MyTableModel class are before any of the other methods.
Please can anyone help me for the solution. Thanks in advance for any help that anyone gives. Also sorry for posting so much text but i wanted to put as much information as possible about the problem as i could.
Thanks again
Talhah Mafawalla