TableSorter NPE exception

F

farseer

Hello,
To implement table sorting in my tables, I am using the following
tableSorter from Sun (note, this is the updated version of tableSorter
that no longer uses a tableMap):
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#"sorting

However, occassionly, I am receiving the exception below but having
difficulty figuring out why this is happening. The error is shown at
the end of my post and always occurs on the following line:

private Row[] getViewToModel() {
if (viewToModel == null) {
int tableModelRowCount = tableModel.getRowCount();
viewToModel = new Row[tableModelRowCount];
for (int row = 0; row < tableModelRowCount; row++)
{
viewToModel[row] = new Row(row); <--THIS IS THE LINE
CAUSING THE EXCEPTION
}

if (isSorting()) {
Arrays.sort(viewToModel);
}
}
return viewToModel;
}

Why would an NPE be occurring here if we are assigning a value???

Here is the exception stack trace:
java.lang.NullPointerException
at
com.dss.streamer.client.table.TableSorter.getViewToModel(TableSorter.java:229)
at
com.dss.streamer.client.table.TableSorter.modelIndex(TableSorter.java:241)
at
com.dss.streamer.client.table.TableSorter.getValueAt(TableSorter.java:278)
at javax.swing.JTable.getValueAt(JTable.java:1771)
at javax.swing.JTable.prepareRenderer(JTable.java:3724)
at
javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1149)
at
javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:541)
at javax.swing.JComponent.paint(JComponent.java:808)
at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
at
javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
 
T

Thomas Weidenfeller

farseer said:
Why would an NPE be occurring here if we are assigning a value???

My random guess would be that you are not looking at the source code
from which your class was compiled.

Clean your build environment, compile and install everything from scratch.

/Thomas
 
T

Thomas Weidenfeller

Thomas said:
Clean your build environment, compile and install everything from scratch.

To avoid any misunderstandings: Only compile and install your
application from scratch, not the JDK, IDE, etc.

/Thomas
 
F

farseer

but i have actually put a try/catch block around that to determine that
that is where it is indeed occurring...
 
M

Menno Holscher

farseer said:
but i have actually put a try/catch block around that to determine that
that is where it is indeed occurring...

Is your definition of viewToModel:
Row[] viewToModel ;
Otherwise you will get a NPE as you stated. Also a superclass of Row may be
acceptable.
 
Joined
Oct 27, 2006
Messages
1
Reaction score
0
Same problem, not solved

Hello,

I have the same NullPointerException problem with the TableSorter class. The row class constructor and the Row[] variable definition are okay. When I debug the code, it looks like the viewToModel variable can happen to be != null int the for constructor for row count 1 to 200, but at row count 201 it is suddenly null.

Any further ideas or suggestions?

Thanks
arkoenne
 
Joined
Nov 16, 2006
Messages
1
Reaction score
0
NPE in TableSorter

To not receive more the NPE in the TableModelSorter follow the steps:

1-)Create an backupToModel array:

private Row[] viewToModel;
private Row[] backupToModel; // <- Add this line

2-)Change the method getViewToModel():

private Row[] getViewToModel() { // <- Change this method
if (viewToModel == null) {
int tableModelRowCount = tableModel.getRowCount();
backupToModel = new Row[tableModelRowCount];
viewToModel = new Row[1];
for (int row = 0; row < tableModelRowCount; row++) {
backupToModel[row] = new Row(row);
}

if (isSorting()) {
Arrays.sort(backupToModel);
}
}
return backupToModel;
}

That´s it... If you have more questions or doubts just send me a message...

Posted in http://forum.java.sun.com/thread.jspa?messageID=4469989 too..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top