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.