• 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

Getting NPE !

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers
My requirement is that I have to fetch data from the database upon a click by the user and display it on the browser. So I'm using JSP, Servlets and Beans. In servlet am connecting to the database(init method) and calling the bean class from the post method. But the method in the bean class is not getting invoked when called from post method. I'm not sure if I have coded correctly or not as I'm newbie to MVC architecture. Making an attempt to learn.

Here's the code:
Servlet:

Bean class

And the error code:

I'm not able to figure out the error in this code. Can anyone help me out?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A NPE will be thrown when an object reference is null while your code is trying to access/invoke it. The first line of the stacktrace points to the line where the NPE is been thrown. In your case it is line 57 of POVPBean.java, inside the getPOVPData() method.

Lookup this line in your code. Check any reference calls in this line. Determine/test if any reference is (possibly) null or not. Once you found a null reference, then fix it accordingly by just instantiating it or by adding a simple nullcheck so that it won't be called.

At first glance, I don't see you getting the actual Connection anywhere. So for example the con.prepareStatement(query) call would throw NPE.

Oh, I would add, don't forget to close the resultset, statement and connection after use. Do it in the finally block. This way you prevent leaking of resources and the related problems.
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly Bauke, the 57th line in the code is the con.prepareStatement(query). But I have put the database connection code in the init method of the servlet. Is this the cause for the NPE??? Should I move that part of the code from servlet to bean class?
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bauke
I have moved the code from the servlet to bean class and now it's working fine. But my doubt is why was it not getting connected when declared in the servlet? Was the connection getting disconnected by the time the control reached the bean class or is the scenario entirely different?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapna Gouri Kalanidhi wrote:Hi Bauke
I have moved the code from the servlet to bean class and now it's working fine. But my doubt is why was it not getting connected when declared in the servlet? Was the connection getting disconnected by the time the control reached the bean class or is the scenario entirely different?



Eventhough you got a connection object in the Servlet, your POVPBean has no idea/access to it. You either need to pass this connection to the method you are trying to invoke or get a new connection in the method itself.

I would also like to say that its probably not a good design to mix your DB related code with the servlet/JSP.

HTH
Ashwin
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapna Gouri Kalanidhi wrote:Exactly Bauke, the 57th line in the code is the con.prepareStatement(query). But I have put the database connection code in the init method of the servlet. Is this the cause for the NPE??? Should I move that part of the code from servlet to bean class?


I have no idea what you're thinking or understanding, but obtaining a connection in the init method of the servlet won't cause the 'con' reference in your bean class automagically become instantiated. You wrote Connection con = null; and in the same block you do a con.prepareStatement(). You have nothing in between what assigns the actual Connection to the 'con' reference.

That said, I would read on about the DAO pattern and also read on about the servlet lifecycle, the connecting and potentially leaking resources. Your initial idea to get a connection in the servlet's init() method and apparently assigning it to an instance variable is a very bad idea.
 
reply
    Bookmark Topic Watch Topic
  • New Topic