Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-07-2003, 07:10 AM   #1
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
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, 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:

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

Old 07-07-2003, 09:30 AM   #2
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
The first thing that jumps out at me in DBTalker is the fact that the class defines a private var ResultSet result, and then the query method creates it's own ResultSet result. I'm betting that the result in the try of the query method is getting clobbered, because it's defined in the try, and the class's private result, which has, as of yet, not been assigned anything, is the object being returned.

I could be wrong, but that is the first thing I'd change.
Old 07-07-2003, 12:08 PM   #3
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30

Thanks for your pointer - I hadn't noticed that I was re-declaring that ResultSet, so thanks very much. As it happens, I then got a new exception as shown below.

java.sql.SQLException: Operation not allowed after ResultSet closed

It turns out that this was because the ResultSet object gets destroyed when the statment, as suggested in the API docs "[close]...Releases this Statement object's database and JDBC resources immediately". However, the docs also state 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", which all emplies that I don't need to bother with the stmt.close() method. whoopee

anyway, thanks



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 05:37 AM
Java JDBC driver mickeyboa Fedora 2 07-23-2005 08:53 AM
java.lang.InstantiationException: java.sql.Connection poeta_boy Programming 2 07-06-2005 09:26 PM
jdbc connection in java balanagireddy Programming 2 07-01-2004 07:42 AM
I was working with jdbc in java in which we need navalarora Linux - Software 0 12-05-2003 07:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration