This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes AbstractTableModel NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "AbstractTableModel NullPointerException" Watch "AbstractTableModel NullPointerException" New topic
Author

AbstractTableModel NullPointerException

Kevin Poole
Greenhorn

Joined: Oct 27, 2008
Posts: 7
Hi

Am getting a NPE when trying to use an AbstractTableModel with a JTable & Oracle JDBC ResultSet. The code I have been using is based on an example in the Core Java Volume 2 book.

The code consistently blows up when the getValueAt method is called with parameters 0 & 0 (ie the first invocation).

I have tried various things, but have condensed the code to the example below.

Any ideas gratefully received.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Welcome to JavaRanch

Unfortunately you can't work out what causes a NullPointerException unless you know which line of code it occurs at. Can you work that out from the stack trace?
Kevin Poole
Greenhorn

Joined: Oct 27, 2008
Posts: 7
Thanks for the welcome and for providing a great resource.

The line it is failing at is the 2nd println in getValueAt below. From experimentation it is as soon as rs.getObject is referenced. So if I comment out the println, then the return statement where it is also referenced fails.

I originally thought it was scope related with the ResultSet object (rs), hence making ResultSetTableModel (the AbstractTableClass) an inner class. As an inner class I have also tried accessing the rset object directly but with the same error. To rule this out, in the example below the ResultSet is a passed parameter to ResultSetTableModel.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I don't see any problem in the Swing code. The NPE seems to support that belief: it is inside the ResultSet code that the NPE is thrown.

Therefore I shall move this thread to JDBC.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
I hadn't realised Rob was moving this thread. I was about to write this
****************************************************************************
It doesn't look as if you are getting a NPE (=NullPointerException) from that line.
System.out won't be null, unless you have done something very silly with it.
The String literal won't be null, and getting a null return from an rs call shouldn't cause a problem; it will simply print out "null".
Try
  • Put in some lines before your println statement which prints whether null.
  • Declare a local variable obj.
  • Your method now will readNote I am using the post-Java5 methods.
    **********************************************************************
    But it's still relevant, so I shall copy and paste it.
    Please check carefully that the ResultSet isn't null.
    Kevin Poole
    Greenhorn

    Joined: Oct 27, 2008
    Posts: 7
    In the console I get the following;

    Trying to connect to the Database
    Connected to Database
    yes it does support scroll
    getColumnCount
    rsmd.getColumnCount returning 2
    getColumnCount
    rsmd.getColumnCount returning 2
    getColumnCount
    rsmd.getColumnCount returning 2
    At end of main ...
    r=0 c=0
    The ResultSet exists: true

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at oracle.jdbc.driver.ScrollableResultSet.getCachedDatumValueAt(ScrollableResultSet.java:2095)
    at oracle.jdbc.driver.ScrollableResultSet.getOracleObject(ScrollableResultSet.java:519)
    at oracle.jdbc.driver.ScrollableResultSet.getObject(ScrollableResultSet.java:932)
    at oracle.jdbc.driver.ScrollableResultSet.getObject(ScrollableResultSet.java:892)
    at OraTop$ResultSetTableModel.getValueAt(OraTop.java:134)
    at OraTop$ResultSetTableModel.getColumnClass(OraTop.java:158)

    Line 134 is the Object obj = rs.getObject(c+1) line.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Don't know. It doesn't look like a line likely to throw a NPE, if the ResultSet is not null, and there is no mention of NPE in the getObject method, but NPE is an unchecked Exception, so it might not be declared.

    Anybody else?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    By "Core Java" do you mean Horstmann and Cornell? Which edition and which page? I might have a copy myself.
    Kevin Poole
    Greenhorn

    Joined: Oct 27, 2008
    Posts: 7
    Yes - Seventh Edition, Chapter 6 Advanced Swing, Tables
    Example 6-11 ResultSetTable.java
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Thank you. I have found my Horstmann and Cornell. Volume II page 380-384?
    The getColumnClass method doesn't appear in Horstmann and Cornell. Is there any possibility that you are passing a null value to that?
    If your database contains a null value, then trying to get its class will throw a NPE. Your getValueAt method is implicated because getColumnClass calls getValueAt.

    I am not sure what to do about that; anybody else got any ideas??
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Thank you. I have found my Horstmann and Cornell. Volume II page 380-384?
    The getColumnClass method doesn't appear in Horstmann and Cornell. Is there any possibility that you are passing a null value to that?
    If your database contains a null value, then trying to get its class will throw a NPE. Your getValueAt method is implicated because getColumnClass calls getValueAt.

    I am not sure what to do about that; anybody else got any ideas??

    ******************************************************************************
    Probably worthwhile going to AbstractTableModel in the API and seeing what it says about implementing that class. Note what it says about the getColumnClass method.
    Kevin Poole
    Greenhorn

    Joined: Oct 27, 2008
    Posts: 7
    Have tried it with getColumnClass commented and it still fails in the same way (just with getColumnClass disappearing from the stacktrace).

    I've also tried the getColumnClass example from here http://www.coderanch.com/t/345807/GUI/java/tablemodel-getcolumnclass-method-produces-null but also to no avail.

    The sql does return values none of which are null. I have also searched Oracle's metalink database without much luck - though I have changed the query slightly to avoid date columns as there is apparently a bug where it returns the wrong data type. This has not made any difference either
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Try overriding getColumnClass to do what the API says, and make its header exactly what it says in the API. But otherwise, I am afraid I don't know. Sorry.

    Anybody else?
    Kevin Poole
    Greenhorn

    Joined: Oct 27, 2008
    Posts: 7
    Thanks for trying. Will have a go at changing getColumnClass.

    Will also try some different versions of the JDBC driver.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    And use the @Override annotation. If you have made a tiny spelling mistake, that will be flagged at compilation time.
    Kevin Poole
    Greenhorn

    Joined: Oct 27, 2008
    Posts: 7
    Hmmm, well I added/amended the following lines;

    public OracleCachedRowSet ocrs;
    <snip>
    ocrs = new OracleCachedRowSet();
    ocrs.populate(rset); // Move the ResultSet into an Oracle RowSet
    <snip>
    ocrs.absolute(r + 1); // change the rset. references to ocrs
    return ocrs.getObject(c + 1);

    And it all worked fine. I just dunno why ... Why get an NPE for a Resultset but not for a RowSet?

    Reason for looking at RowSets (which I haven't used before) is that I noticed in the Eighth Edition of Core Java II they seem to have switched their examples to RowSets.

    So I have a fix / workaround, just can't explain it?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Peculiar. Can't see why that made any difference. RowSet has been available since J2SE1.4.

    Does anybody else know, please?
    laila NiHai
    Ranch Hand

    Joined: Oct 18, 2010
    Posts: 35
    Hello,

    I realized that this is an old thread, but it's relevant to the problem that i'm having...

    Basically, i'm using the ResultSetTableModel and have override the getColumnClass() to return class based on the sql datatype int...(NUMBERIC, DATE, TIMESTAMP...ect...) however, upon first load of the table, the getColumnClass() of AbstractTableModel is called, instead of my getColumnClass(). Why is that?

    Please help. Thanks in advance.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: AbstractTableModel NullPointerException
     
    Similar Threads
    Slight JTable problem
    Cell showing junk chars after mouse click
    removing a scrollpane, yet still see it
    String index out of bound exception in JTable
    problem with insert and select