This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Simple JDBC problem - Connection throws Nullpointerexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Simple JDBC problem - Connection throws Nullpointerexception" Watch "Simple JDBC problem - Connection throws Nullpointerexception" New topic
Author

Simple JDBC problem - Connection throws Nullpointerexception

Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
Hi guys,
I'm am currently trying to learn Java and want to connect to a Postgresql database.
I am using a simple JSP page and a bean which contains all database functions. This is set up in Tomcat and JDK/1.6.0. Everything works fine but the connection itself, for some reason it throws a null value.
I suspect that the problem is the connection configuration, in tomcat/conf/context.xml.
Connecting to the database from terminal works fine, using psql -h etc...

This is what I have:

tomcat/conf/context.xml


tomcat/webapps/ROOT/WEB-INF/web.xml


tomcat/webapps/ROOT/WEB-INF/classes/bean/db.java



tomcat/webapps/ROOT/login.jsp




Running login.jsp returns error here:


Error output:


What am I doing wrong? Help is very much appreciated.
Thank you.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
Hi Balu and thank you for the quick reply. I changed

to

However, this did not seem to solve the problem. Error message is still the same.
Do you have any other ideas what might be wrong?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



The code above throws the exception when creating connection and How could one possibly know what it is unless you print it out.
Empty Catch() statement are programmers 'Devil' , it hides everything and shows everythig as normal


Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
Hi again,
I did not get the e.printStacktrace() to work (compilation error, missing symbol), but this code compiled fine:



However, the only difference from the previous error is that the jsp error row (out.println(db.loginUser("adam","test")); ) wasn't mentioned.
Still getting this stacktrace:



The NullpointerException is still pointing to the conn.createStatement(); row.
What can I do?

Thank you for your patience.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


I did not get the e.printStacktrace() to work (compilation error, missing symbol), but this code compiled fine:


Use e.printStackTrace() outside System.out.println , as it returns nothing..

OK , Add the Proper Exceptions handlers in both of you methods.



The NullpointerException is still pointing to the conn.createStatement(); row.


Its because is conn object is null. So should look in constructor itself where the conn is intialised. Add Exception as below.




Please post complete Sys out log on whats happening before the exception.
Refer this this for checking basic setup

Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
I tried the code with the new catch statements. The strange thing is that it no longer returns an error at all, the page loads fine and the function returns false. (It should return true)

This is the complete code for db.java now:


I don't know if there is a way to make the function print my query, but System.out.println did not work (nothing was printed).
Though, I am pretty sure the sql is ok, because when I changed the last return to true, the whole function returned true.

Btw, the reference looked good, I have read the part about Postgresql but I don't think I have missed anything in the setup.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




BTW , did you call close() to close the connection. Otherwise you get such abnormal behaviour
don't know if there is a way to make the function print my query, but System.out.println did not work (nothing was printed).



Is it not in the console or catalina.out ? where did you check.. did you route to different log ?

IMO this is not the correct design at all. you can have db.java as utility class for serving up connections and what ever you use in jsp:usebean should be a bean which follows bean standards.
Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
I added the finally clause to the function like this:



This returned an error at conn.close(); as conn is null.

I have also attached the last log entry in catalina.out to this post, maybe you could translate to english and see what is wrong
Catalina.out

To me, it still looks like a configuration problem, like if it doesnt find the database host.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


To me, it still looks like a configuration problem, like if it doesnt find the database host.


Thats 100% right. but we have been trying to catch the error and found out whats the misconfiguration. I wonder how your code manages to escape exceptions and show error on different line on different run..

I m not able to donwload the file Catalina.out. Did you look in to that ? found anything ? Is it printing SOPs ? Unless the i see the SOP or log , cant predict anything..
Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
Yes, the catalina.out shows quite the same as the error message. Though, it prints the "Initializing datasource"

Here:

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



SEVERE: Parse error in context.xml for /manager



context.xml is not properly "well -formed" Check out how to configure context.xml and meanwhile post your complete context.xml
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Adam,
A few problems:
1) You are ignoring the SQL Exception thrown on creating the connection. It's not valid to continue so you might as well take the user to an error page when this happens.
2) It's traditional to use an if statement in the finally block.

3) You need to close the connection when done with it not after the first query.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Adam Haglund
Greenhorn

Joined: Feb 19, 2009
Posts: 7
Thank you so much, I finally got everything working.

Though, I still get a nasty error in catalina.out. Should I be worrying about it, and how do I fix it?
Also, my context.xml looks exactly like in my first post.

Catalina.out coming here...

 
wood burning stoves
 
subject: Simple JDBC problem - Connection throws Nullpointerexception
 
Similar Threads
org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context
exception - name jdbc is not bound in this context
Database connectivity issue
java.sql.SQLException: No suitable driver
BasicDataSource cannot be cast to javax.naming