Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AbstractTableModel NullPointerException

 
Kevin Poole
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51415
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 51415
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By "Core Java" do you mean Horstmann and Cornell? Which edition and which page? I might have a copy myself.
     
    Kevin Poole
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes - Seventh Edition, Chapter 6 Advanced Swing, Tables
    Example 6-11 ResultSetTable.java
     
    Campbell Ritchie
    Sheriff
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for trying. Will have a go at changing getColumnClass.

    Will also try some different versions of the JDBC driver.
     
    Campbell Ritchie
    Sheriff
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And use the @Override annotation. If you have made a tiny spelling mistake, that will be flagged at compilation time.
     
    Kevin Poole
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51415
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic