0

I recently started java programming. I'm getting java.lang.NullPointerException in my serlvet. Code is posted below. can anyone help me please?

I'm using mysql- username, password, etc are correct.I can see the values in table.

message in console -

java.lang.NullPointerException

at j2ee.Authenticate.processRequest(Authenticate.java:49)

at j2ee.Authenticate.doGet(Authenticate.java:93)

Code -

package j2ee;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "Authenticate", urlPatterns = {"/Authenticate"})
public class Authenticate extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String user = request.getParameter("userId");
    String pwd = request.getParameter("passwd");
    String dbuser = null;
    String dbpwd = null;

    try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/j2ee","root","password");

    String query = "select username, password from user_info where username=? && password=?";

     PreparedStatement p = con.prepareStatement(query);
     p.setString(1,dbuser);  
     p.setString(2,dbpwd);  

     ResultSet rs=p.executeQuery();





     if(dbuser.equals(user) && dbpwd.equals(pwd))
    {
            String s=request.getParameter(user);
            out.println("Welcome" +s); 
            RequestDispatcher rd = request.getRequestDispatcher("Home");
            rd.include(request, response);

    }   
    else
    {
            out.println("Please enter a valid Username and Password!"); 
            RequestDispatcher rd = request.getRequestDispatcher("Login");
            rd.include(request, response);

    }

}
    catch (IOException e)
    {
    System.out.println(e);
    } catch (ClassNotFoundException e) {
        System.out.println(e);
    } catch (SQLException e) {
        System.out.println(e);
    } catch (ServletException e) {
        System.out.println(e);
    }


}
  • 2
    Have you tried stepping through your code to determine what value is null? I know eclipse has really good support for debugging. (been a while since I used so this could have changed.) – Freddy Jul 10 '14 at 17:00
  • 1
    Do you see where you have the line `at j2ee.Authenticate.processRequest(Authenticate.java:49)`? That is telling you that the problem is in line 49 of `Authenticate.java`. The `NullPointerException` tells you that you have a variable that is null. This might help you see whereabouts the problem comes from. – glenatron Jul 10 '14 at 17:01
  • @glenatron, "The NullPointerException tells you that you have a variable that is null." No it doesn't. It tells you that you used the dot operator on an object that is null, like `dbuser.equals(` where dbuser was `null`. – developerwjk Jul 10 '14 at 22:17
  • @developerwjk it's a while since I used Java, but when it was one of my main programming languages you could assign objects to variables. Saying things like `DBUser dbuser = new DBUser( connectionString );` was the way we used to do it back then. Is that no longer the case? – glenatron Jul 11 '14 at 09:34
  • You can do that. But what was done here was `String dbuser = null; if(dbuser.equals(user))` hence the null pointer. dbuser being null is fine as long as you don't use the dot operator. For example `if("somename".equals(dbuser))` would not throw a null pointer exception. Simply reversing the comparison can avoid the exception many times. – developerwjk Jul 11 '14 at 20:09

3 Answers3

0

dbuser and dbpwd are initialized to null and never assigned a value. Looks like you are trying to validate a login against a table, but you are never reading the results from the ResultSet object. I think you want to do this:

String query = "select username, password from user_info where username=? && password=?";

     PreparedStatement p = con.prepareStatement(query);
     p.setString(1,user);  
     p.setString(2,pwd);  

     ResultSet rs=p.executeQuery()

     if ( rs.next() ) {
        dbuser = rs.getString(1);
        dbpwd  = rs.getString(2);
      }
     // etc ...

Maybe read up on how to debug in an IDE and how to use result sets

OldProgrammer
  • 10,029
  • 3
  • 24
  • 39
  • i got this much , but what should i do next? how do i validate the result?.... –  Jul 10 '14 at 17:26
  • 1
    If you have a new,different question, then post a new question. This is not a forum for ongoing, open-ended tutoring. Maybe spend some time reading the [Java Tutorials](http://docs.oracle.com/javase/tutorial/) first, then read up on JDBC, etc. There are endless resources on the web. Learn how to use them. – OldProgrammer Jul 10 '14 at 17:31
0

You misuse the prepareStatement function, please

String query = "select username, password from user_info where username=? && password=?";

PreparedStatement p = con.prepareStatement(query);
p.setString(1,dbuser);   //wrong, you are replacing ? with null
p.setString(2,dbpwd);    //wrong, you are replacing ? with null

You should count the number of row instead.

String query = "select count(*) from user_info where username=? && password=?";
p.setString(1,user);
p.setString(2,pwd);  
ResultSet rs=p.executeQuery()

int result = rs.getInt(1);
if(result >= 1){ 
   ..authen OK
}
else{
   ..authen failed
}
tom87416
  • 522
  • 4
  • 9
0

You never set the value of dbuser and dbpwd ,you have initialized them with null but you used it in PreparedStatement.

SparkOn
  • 8,193
  • 3
  • 23
  • 28