-1

*Well I have created a basic servlet program Where if user give their USN number correct it will fetch data from database else it will redirect to error.html Whenever I Enter USN number it shows NullPointerException .Since I am using Eclipse There was No sign of error. Note:I have Also Created a html file to enter the USN Number.

Code:

package com.abc.error;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetResult extends HttpServlet {
 /**
 * 
 */
 static final long serialVersionUID = 1L;
 Connection con= null;
 PreparedStatement pstmt=null;
 ResultSet res=null;
 public void init(){
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE","system","system");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

@Override
public void service(HttpServletRequest request, HttpServletResponse response) {
    try
    {   
        pstmt = con.prepareStatement("SELECT * FROM VTURESULT_AUG WHERE USN =?");
        String key = request.getParameter("USN");
        if(key.length()!=12){
            response.sendRedirect("/ServletWithError/error.html");
        }
        else
        {
            pstmt.setString(1,key);
            res =pstmt.executeQuery();
            if(res.next())
            {
                String usn = res.getString(1);
                String name = res.getString(2);
                int m1 = res.getInt(3);
                int m2 = res.getInt(4);
                int m3 = res.getInt(5);
                PrintWriter pw =response.getWriter();
                pw.println(usn+" "+name+" "+m1+" "+m2+" "+m3);
            }
        }
    }
    catch(SQLException | IOException e)
    {
        e.printStackTrace();
    }
  }
}

Output:

java.lang.NullPointerException
com.abc.error.GetResult.service(GetResult.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Bankimh
  • 1
  • 3
  • @Filburt No this Does not Solve my problem... – Bankimh Nov 20 '19 at 16:55
  • The link from Filburt should help you and don't miss the [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Tom Nov 20 '19 at 17:22

1 Answers1

0

The stacktrace says the NPE is at this line if the posted code's line numbers match your actual code:

pstmt.setString(1,key);

You can verify this by setting a breakpoint on that line.

I can't see your actual code, so there are a couple of things you should check using more breakpoints:

  1. Is con null? If so, your init() method may not be getting called, or it's throwing an exception higher up in your log that you're not seeing.

  2. Move your PreparedStatement declaration down to where it's being used - declaring it outside the function isn't helping you at all in this case:

    PreparedStatement stmt = con.prepare(...);

  3. And of course, verify that key is non-null.

spork
  • 1,185
  • 1
  • 11
  • 17
  • If `con` would be `null`, it would've thrown the NPE on line 29. If `key` would be null, the call stack would be at least one deeper. The error indicates that `pstmt` is `null`. – Ivar Nov 20 '19 at 16:36
  • @spork I have done as you have mention above but Still i m getting that same error – Bankimh Nov 20 '19 at 16:50
  • @Ivar agreed...if OP is posting the code as of the time the stacktrace was generated. I've learned not to assume that. – spork Nov 20 '19 at 16:58
  • @Bankimh You've set breakpoints and confirmed that `con`, `stmt`, and `key` are all non-null at runtime? If so, you can't be getting the same error. – spork Nov 20 '19 at 17:00