This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Getting NPE ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Getting NPE ! " Watch "Getting NPE ! " New topic
Author

Getting NPE !

Swapna Gouri Kalanidhi
Ranch Hand

Joined: Mar 14, 2008
Posts: 107
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?


Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Mar 14, 2008
Posts: 107
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

Joined: Mar 14, 2008
Posts: 107
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?

Ashwin Pai
Ranch Hand

Joined: May 20, 2008
Posts: 90
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


The only way to make your dream come true is to wake up.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting NPE !
 
Similar Threads
Data Alignment
Parameter Index out of range !
SQLException:Parameter Index out of Range
exception while running code
The server encountered an internal error () that prevented it from fulfilling this request