TableSorter NPE exception

Discussion in 'Java' started by farseer, Jun 23, 2005.

  1. farseer

    farseer Guest

    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)
    farseer, Jun 23, 2005
    #1
    1. Advertising

  2. farseer wrote:
    > 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


    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Jun 23, 2005
    #2
    1. Advertising

  3. Thomas Weidenfeller wrote:
    > 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

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Jun 23, 2005
    #3
  4. farseer

    farseer Guest

    but i have actually put a try/catch block around that to determine that
    that is where it is indeed occurring...
    farseer, Jun 25, 2005
    #4
  5. farseer wrote:

    > 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.
    --
    Groeten van/regards

    Menno
    Menno Holscher, Jun 25, 2005
    #5
  6. farseer

    farseer Guest

    Yes, that is how viewToModel is defined.
    farseer, Jun 27, 2005
    #6
  7. farseer

    arkoenne

    Joined:
    Oct 27, 2006
    Messages:
    1
    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
    arkoenne, Oct 27, 2006
    #7
  8. farseer

    suvacodecobra

    Joined:
    Nov 16, 2006
    Messages:
    1
    Location:
    Brazil
    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..
    suvacodecobra, Nov 16, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert
    Replies:
    3
    Views:
    572
    Tris Orendorff
    Oct 6, 2003
  2. Tim
    Replies:
    1
    Views:
    441
    Tim Ward
    May 19, 2004
  3. VisionSet

    Sun's swing TableSorter

    VisionSet, Jun 2, 2004, in forum: Java
    Replies:
    8
    Views:
    813
    Shane Petroff
    Jun 7, 2004
  4. cm
    Replies:
    2
    Views:
    2,004
  5. Steve R. Burrus

    It's still about the NPE.

    Steve R. Burrus, Mar 25, 2005, in forum: Java
    Replies:
    5
    Views:
    325
    Ryan Stewart
    Mar 27, 2005
Loading...

Share This Page