Help required.

Arun Boraiah

Ranch Hand

Posts: 233

posted 10 years ago

On of the application making a connection to database is throwing null pointer exception. When traced following is my observation. I am able to get the connection object. I used isClosed() to check the connection which is returning false. Hence the connection is valid. but when i use createStatement() method it is returning null. What would be the possible reason.... Thanks in advance for the help...

You're going to need to post some code and the stack trace, and point out the line where the NullPointer is being thrown. (try to keep the information short and concise)

Until then we'd just be guessing.

Arun Boraiah

Ranch Hand

Posts: 233

posted 10 years ago

Thanks for response David O'Meara. what i had written in my posting is about what i observed. In short i am able to get the Connection object which is valid. But using the connection object when i calling createStatement() method which is returing null instead of Statement object.

What would be possible cause of getting null instead of statment object?

Sharing is learning

stu derby

Ranch Hand

Posts: 333

posted 10 years ago

What would be possible cause of getting null instead of statment object?

What would be possible cause of getting null instead of statment object?

Hard to say given the information you have provided. Could you post the stack trace? And the code you are trying to run? It should help you get an answer since, as David points out, without it we can only guess.

In your first post, you imply that stmt is going to be null a this point; if so, you should be getting a NullPointerException on this line. Do you?

A stack trace would be helpful.

Arun Boraiah

Ranch Hand

Posts: 233

posted 10 years ago

i am printing statement. Object in system.out.println to shown that statement object is null. I am not getting any stack trace. Since in the code i was using statement object I was geting null pointer exception. To debug i just checked the connection object with isClosed method, which is returing false, which indicate that connection is right... but just not getting the statment object......

Originally posted by Arun Boraiah: i am printing statement. Object in system.out.println to shown that statement object is null. I am not getting any stack trace. Since in the code i was using statement object I was geting null pointer exception. To debug i just checked the connection object with isClosed method, which is returing false, which indicate that connection is right... but just not getting the statment object......

Null is not the same as a nullPointerException.

Statement.toString() can return anything, literally anything at all; it's behavior is not defined by the JDBC standard and therefore printing a Statement object is not guaranteed to be useful or at all diagnostic. (The driver implementer may do something useful/diagnostic, but is not required to do so). Returning "null" is a unusual, but not wrong, and does not mean that the Statement is null.

In other words, it looks like your code is working fine, at least to this point.

Do you have more than one stmt declaration in your program. What I see you have declared statement inside try {} block

Which cleary leads that it does not have any scope outside your try{} block then how are you returning a stmt from your method.

If you do not have other declaration then how does your code get compile. If yes then you would return a null statement from your method. and further if you use same You will get a NullPointerException

Shailesh

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein

Arun Boraiah

Ranch Hand

Posts: 233

posted 10 years ago

Hi All, Apologies not responding in time, I was able to resolve the problem. The cause for the problem was not with in the code, but it was in environment setting. Classpath was pointing to wrong database driver. On correcting the classpath the application restored to it�s normal behavior.