java.lang.NumberFormatException: null

1

I'm starting with Java Servlets doing a program with a login. I'm fine but when I click on the Login button I get this error

  

java.lang.NumberFormatException: null     java.lang.Integer.parseInt (Integer.java:542)     java.lang.Integer.parseInt (Integer.java:615)     servlets.Login.doPost (Login.java:57)     javax.servlet.http.HttpServlet.service (HttpServlet.java:650)     javax.servlet.http.HttpServlet.service (HttpServlet.java:731)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)

I know it has to do with line 57 but I can not understand what is wrong. This is the servlet login code

protected void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    int n= Integer.parseInt(request.getParameter("Cedula"));
    String p=request.getParameter("Contraseña");

    if(metodos.Validate.checkUser(n, p)) {
        RequestDispatcher rd=request.getRequestDispatcher("Welcome");
        rd.forward(request,response);
    } else {
        out.print("Sorry username or password error");
        RequestDispatcher rd=request.getRequestDispatcher("index.html");
        rd.include(request,response);
    }

    out.close();
}

This is the method checkuser

public static boolean checkUser(int Cedula,String Contraseña) {
    boolean existe=false;
    try {
        Conexion db=new Conexion();
        Connection cn=db.Conectar();
        Statement st=cn.createStatement();
        PreparedStatement ps =cn.prepareStatement("select * from CLIENTE where Cedula=? and Contraseña=?");

        String cedula = String.valueOf(Cedula);
        ps.setString(1, cedula);
        ps.setString(2, Contraseña);
        ResultSet rs =ps.executeQuery();

        if(rs.next()) {
            existe = true;
            /*
            this.identificacion=rs.getInt("identificacion");
            this.contraseña=rs.getString("contraseña"); 
            cliente=true;
            */
        } else {
            existe = false;
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
    return false;
}  

And this is my class Conexión

public class Conexion {
    private String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String url="jdbc:sqlserver://localhost:1433;databaseName=HOTEL";
    private String user="sa";
    private String pass="progra";

    public Conexion() {        
    }

    public Connection Conectar() {
        try {
            Class.forName(driver);
            return (DriverManager.getConnection(url,user,pass));
        } catch (Exception e) {
        }

        return null;
    }

    public void Desconectar(Connection cn) {
        try {
            cn.close();
            } catch (Exception e) {
        }
    }
}
    
asked by Legmaria 17.04.2017 в 06:03
source

4 answers

1

This exception throws when you want to parse a non-numeric character whole, or a null , which is your case: java.lang.NumberFormatException: null java.lang.Integer .parseInt (Integer.java:542)

Make sure that on your html or jsp page you are using the name of your variable Cedula well, since evidently your servlet is not receiving any value.

    
answered by 17.04.2017 в 14:49
0

You have problems when paging from String to Int. There is no try{}catch and therefore if there is an exception such as the value is a non-numeric character or a value null , this finally return an error.

Therefore, what you must do is to add a try{} catch(){} so that it can be executed without problems.

Here's the code below:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {  
    try{

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  

        int n= Integer.parseInt(request.getParameter("Cedula")); 
        String p=request.getParameter("Contraseña");  


        if(metodos.Validate.checkUser(n, p)){  
            RequestDispatcher rd=request.getRequestDispatcher("Welcome");  
            rd.forward(request,response);  
        }else{  
            out.print("Sorry username or password error");  
            RequestDispatcher rd=request.getRequestDispatcher("index.html");  
            rd.include(request,response);  
        }  

    out.close();  
    } catch (Exception e) {
        System.out.println("Hubo un error");
    }
}  

I hope that with this I clarify the doubts and you can understand where your error comes from. Greetings!

NOTE:

Another way for you to avoid this type of errors in case you are working with web pages, is that before sending the form, corroborate a javascript information to send, or, said input is not type "text", but "number".

Ex: <input type="number" id="Cedula" name="Cedula">

Success!

    
answered by 17.04.2017 в 14:29
0
  

NumberFormatException Launched to indicate that the application has tried to convert a   string to one of the numeric types, but the string does not have the   appropriate format.

The NumberFormatException exception is generated in this line of code at try to convert a string that does not have a numeric format using Integer.parseInt() :

int n = Integer.parseInt(request.getParameter("Cedula"));

You have to ensure the value of request.getParameter("Cedula") is numeric to avoid the problem.

Examples:

Launch NumberFormatException :

Integer.parseInt(null);
Integer.parseInt("");
Integer.parseInt("a97");
Integer.parseInt("@#$");

Correct conversion:

Integer.parseInt("12");
Integer.parseInt("0");
Integer.parseInt("9245");
    
answered by 20.06.2017 в 06:08
-1

The Exception shown NumberFormatException is clear, the method parseInt (String value) of the class Integer wait for a string with a numeric value, if this value is not numerical, throw the Exception

Integer.parseInt("") /* Lanza Exception*/
Integer.parseInt("A") /* Lanza Exception*/
Integer.parseInt("123") /* No Lanza Exception*/

The possible error is that the parameter celula of request has a null or non-numeric value. To solve this, you would have to validate that the entry is a numeric value. One option would be to use Regular Expression. ^[1-9]\d*$ .

if(request.getParameter("Cedula")!=null){
  if(request.getParameter("Cedula").matches("^[1-9]\d*$")){
    System.out.println("Entrada Correcta");
  }
  else
    System.out.println("Entrada Incorrecta");
}
else
    System.out.println("Verificar los Datos");
    
answered by 17.04.2017 в 06:15