Forums Register Login

Getting NPE !

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1141 times.
Similar Threads
Parameter Index out of range !
SQLException:Parameter Index out of Range
The server encountered an internal error () that prevented it from fulfilling this request
exception while running code
Data Alignment
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:43:59.