#1
  1. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    111

    java.lang.NullPointerException in JDBC test


    Okay, so this isn't strictly server-side, but I'll post anyway..I am experimenting with Java for the first time - in particular with JDBC. I have managed to connect to my database using the code posted here And that works just fine.

    Now I'm trying to create a class whose constructor will create a connection to the db, and then provide it with a query method to run a query and return a reault object.

    I have a second programme which instantiates an object from this class def, passes a query string to it, receives the returned ResultSet and attempts to loop through it and print out the results.

    My base class DBTalker is posted here . The 'calling' class, DBTest.java is shown here

    Both classes compile without error, but I get the following exception at runtime:

    Connected to brezhnev
    Managed to create statement
    Cound't get through the results:
    java.lang.NullPointerException

    Can anyone point out any obvious errors and/or bad practices please?

    christo
    This is me: http://chris.uk.com
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    I actually tried the same thing not so long ago. Whenever you close a statement, the associated ResultSet is closed as well. This means that when you try 'while(res.next())', the ResultSet has disappeared (i.e. it's null), so you get the exception. What I ended up doing was to create a class that will create a Connection to my database (like your DBTalker) and then each class that calls it would have to create its own Statement.

    ~ishnid
  4. #3
  5. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    111

    good call


    ishnid,

    Well thanks for the pointer.. you were right on the mark. I had a look in the API docs, which state that the close method on a Statement will

    "Release this Statement object's database and JDBC resources immediately"

    However the docs also point out that

    "A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed."

    so there really is no need to call the statement.close() method at all.


    Anyway, thanks for the pointer,

    shokran,
    christo
    This is me: http://chris.uk.com
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    You can include a close() method in your DBTalker that closes any Statements and/or Connections that are open. This will be more efficient than waiting for garbage collection.

    Another option is to copy the data from the ResultSet and return the copy, rather than return the ResultSet itself. This is probably the better approach from an OO point of view, but probably isn't as efficient.

IMN logo majestic logo threadwatch logo seochat tools logo