LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-07-2003, 06:10 AM   #1
chr15t0
Member
 
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, 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
 
Old 07-07-2003, 08:30 AM   #2
TheLinuxDuck
Member
 
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, 11:08 AM   #3
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
TheLinuxDuck,

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


christo
 
  


Reply


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 04:37 AM
Java JDBC driver mickeyboa Fedora 2 07-23-2005 07:53 AM
java.lang.InstantiationException: java.sql.Connection poeta_boy Programming 2 07-06-2005 08:26 PM
jdbc connection in java balanagireddy Programming 2 07-01-2004 06:42 AM
I was working with jdbc in java in which we need navalarora Linux - Software 0 12-05-2003 06:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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