Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2006

Tip: Looking for answers? Try searching our database.

scrollRectToVisible generating NullPointerException intermittently

Thread view: 
John Prestel - 21 Jun 2006 20:56 GMT
This is a VERY intermittent problem. I'm testing scrolling around in a
JTable/JScrollPane.  I use 2 for loops to scroll between the first 20 rows in
the table with 200 repetitions. There is no pattern for the Exception
generation. Most of the time it works, and sometimes it doesn't.

The function that calls scrollRectToVisible does 2 things.

First, it changes an integer in the CellRenderer object (custom renderer,
implements TableCellRenderer). When drawing the JTable, my renderer simply
checks the row # of the cell against this integer, and if they match it
changes the background color from white to green. So there is a change to the
graphics, but everything else about the table and window stay the same
(dimensions, etc.).

Second, it uses getCellRect() method of JTable to calculate the rectangle for
the first cell in row X and then calls scrollRectToVisible to scroll to that
row.

Here is the relevant code. If you think it's necessary to see the code for
the Cell Renderer, Table Model and/or the Table itself (the way it's
constructed) let me know and I'll update the thread with it.
####################################

This is loop used for testing:

for (int i=0; i<200; i++)
{
    for (int j=0; j<20; j++)
    {
         frame.highlightRow(j);
    }
}

And here is the function highlightRow ( int )

public void highlightRow(int row)
{  
    // Indicate the selected row to the Cell Renderer
    ( (ScriptDisplayCellRenderer) myCellRend).setSelectedRow(myTable,
selectedRow);

    // Scroll the viewport to make the row visible
    myTable.scrollRectToVisible( myTable.getCellRect(row, 0, false) );
}

#####################################
I stupidly didn't copy the stack trace the last time it generated the
exception.  

However, I did get another anomaly that may be related. It also randomly
occurs.

Something more... I was running it just now to get a stack trace to show you
guys and something else just kicked off a NPE. The Exception did not kill the
program... it kept going, but I'm not pleased it popped up at all. Here's the
trace:

java.lang.NullPointerException

    at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1316)

    at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:636)

    at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:770)

    at javax.swing.plaf.basic.BasicTextUI.update(BasicTextUI.java:749)

    at javax.swing.JComponent.paintComponent(JComponent.java:541)

    at javax.swing.JComponent.paint(JComponent.java:808)

    at javax.swing.CellRendererPane.paintComponent(CellRendererPane.java:134)

    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1150)

    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.paintChildren(JComponent.java:647)

    at javax.swing.JComponent.paint(JComponent.java:817)

    at javax.swing.JViewport.paint(JViewport.java:722)

    at javax.swing.JComponent.paintChildren(JComponent.java:647)

    at javax.swing.JComponent.paint(JComponent.java:817)

    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)
John Prestel - 27 Jun 2006 13:20 GMT
^bump
TomWest - 27 Sep 2006 22:22 GMT
I just came across the same problem, and found a related bug in the Sun
database.

Anyway, that problem was related to scrollRectToVisible not being called in
the UI thread.  When I changed my program to do so, the problem immediately
disappeared (and I was having it consistently).

I replaced

           messageTable.scrollRectToVisible (cellRect);

with
           Runnable scrollRectToVisibleMethod = new Runnable ()
           {
               public void run ()
               {
                   messageTable.scrollRectToVisible (cellRect);
               }
           };
           SwingUtilities.invokeLater (scrollRectToVisibleMethod);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.