jamesblunt 0 Newbie Poster

Hi!

I try the following with a javax.swing.JTable:

- a custom TableModel (extending from DefaultTableModel) that allows adding/deleting rows by activating a checkbox in a javax.swing.JTree
- a javax.swing.table.TableRowSorter (since JDK 1.6) that allows sorting the rows

Initially, there is no checkbox marked within the JTree. So, the table model contains NO element and the underlying "java.util.Vector" "dataVector" of the "DefaultTableModel" variable is empty. At that time, the "TableRowSorter" already has been registered via:

TableRowSorter<TableModel> sorter =
	    	new TableRowSorter<TableModel>(projectTableModel);
...
projectTable.setRowSorter(sorter);

Enclosed the "getValueAt()" and "getColumnClass()" methods of my table model:

@Override
public Class<?> getColumnClass(int c) {
    return getValueAt(0, c).getClass();
}
@Override
public Object getValueAt(int row, int column) {
	if (dataVector.size() > 0) {
    	Vector rowVector = (Vector) dataVector.elementAt(row);
    	
    	Project project = (Project) rowVector.elementAt(1);
    	Group group = (Group) rowVector.elementAt(2);
	    	
    	switch (column) {
    		case 0:
    			return rowVector.elementAt(0);
    		case 1:
    			return project.getId();
    		case 2:
    			return project.getName();
    		case 3:
    			return group.getId();
    		case 4:    			
    			return group.getName();
    		case 5:
    			return group.isAdmin() ? true : false;
    		case 6:
    			return group.getUsers().length;
    		default:
    			return "";
	}
    	} else
		return "";
}

When first sorting an empty table model by pressing the corresponding column header in the JTable view and afterwards activating a checkbox within the JTree (that adds rows to the table model), I get the following error:

java.lang.NullPointerException
	at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:501)
	at javax.swing.JTable.convertRowIndexToModel(JTable.java:2611)
	at javax.swing.JTable.getValueAt(JTable.java:2686)
	at javax.swing.JTable.prepareRenderer(JTable.java:5703)

So, I wanna reach the following: sorting should just be possible, when the underlying table model does contain at least >0 rows.

I already read the following thread, but I couldn't see any hints solving my problem: http://www.daniweb.com/forums/thread329192.html

Maybe, you can help me?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.