Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Perpared Statemt is throwing NullPointerException after getting context from ServletContextListener?

 
Anuj Kalra
Greenhorn
Posts: 10
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
So I have a Listener that implements ServletContextListener. in the contextInitialized method that makes a database connection with Oracle 11g schema.

After establishing a database connection in the listener (I do get a successful println that I wrote in case the connection was made), I set Attributes to a servletcontext


In my jsp file under a scriplet tag I make an instance of the Connection interface and initialize it with context's attribute:


So in the output i am getting "Connection Made" but i am getting a NullPointerException from the Prepared Statement line; (I am not getting "preparedstatement created" in the output.

Why is this prepared statement causing problems?
Please Help!
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Shouldn't you be using the getAttribute method here? After all you used setAttribute to put the connection into the application context.
 
Anuj Kalra
Greenhorn
Posts: 10
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Shouldn't you be using the getAttribute method here? After all you used setAttribute to put the connection into the application context.


You are right; it works now!
I feel like such an idiot :
application is the implicit variable for ServletContext in JSP and i was calling getContext() from it..
It works perfectly fine now!

Thanks so much Mr. Paul; I was stuck on this for 2 days but I can continue with my work finally.

Head First People were so right; You people here are just AMAZING!!!
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't look like a good design to me. If you are opening a connection in the listener then that connection is going to stay open until the application shuts down; it will never be returned to the connection pool. Connections should be opened in the DAO object and then closed as soon as possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic