• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using vectors after extending on AbstractTableModel

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there.

I have been working on this problem for couple of days now and seem to be getting nowhere. I am using an object of the DefaultTableModel class to display my information from vectors on a table i.e

DefaultTableModel tableModel;

Then I initialise the table model by doing the following:

Vector vColumnNames = new Vector();
Vector vData = new Vector();
tableModel = new DeafaultTableModel(vData, vColumnNames);
JTable table = new JTable(tableModel);

All this works fine for displaying the information but i am trying to make certain cells not editable.

I tryed the following code but it kept giving me an NullPointerException error on line 20 where it states "return vData.size()". By the way this is an inner class and I changed the code in the outer class to:

MyTableModel mtm = new MyTableModel();
JTable table = new JTable(mtm);

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 occusr 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("--------------------------");
}

}

Sorry for posting so much code. Thanks in advacnce for any help.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From where does the method get called? Without taking a deeper look, I'd suspect that it gets called from the DefaultTableModel constructor, which gets executed before your constructor code. If in doubt, post the full stacktrace...
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for looking into the problem. The stack trace as requested is bellow:

init:
deps-jar:
Compiling 1 source file to I:\Other Stuff\Java\My Projects\OrderManagementSystem\build\classes
Note: I:\Other Stuff\Java\My Projects\OrderManagementSystem\src\ordermanagementsystem\OrderForm.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at ordermanagementsystem.OrderForm$MyTableModel.getRowCount(OrderForm.java:136)
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:122)
at ordermanagementsystem.OrderForm.<init>(OrderForm.java:61)
at ordermanagementsystem.OrderForm.main(OrderForm.java:114)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

I think your right, when the class is called by making an object of it using

MyTableModel mtm = new MyTableModel();

the compiler comes to the first line of the constructor i.e.

public MyTableModel(){ //This line
...
}

than jups to the following method at wich point it gives the null poiter exception:

public int getRowCount() {
return vData.size();
}

To find this out I used step through.

Sorry for posting more than one question but i was wondering if i should use the vectors in this class or change the vColumnNames to a String[] and vData to an Object[][].

Thanks again for the response to my question.

[ January 08, 2007: Message edited by: Talhah Mafawalla ]
[ January 08, 2007: Message edited by: Talhah Mafawalla ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic