• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple JDBC problem - Connection throws Nullpointerexception

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



 
Adam Haglund
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the catalina.out shows quite the same as the error message. Though, it prints the "Initializing datasource"

Here:

 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adam Haglund
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic