简体   繁体   中英

java.lang.NumberFormatException: null with Integer.Parseint()

*when I try to run my JSP on the server I encounter this problem... the servlet is:

@WebServlet({"/Registrazione" , "/Registrazione.jsp"})
public class Registrazione extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Registrazione() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        ClienteDAO d=new ClienteDAO();

        String idCliente=request.getParameter("userid");
        String nome=request.getParameter("nome");
        String cognome=request.getParameter("cog");
        String partitaIva=request.getParameter("piva");
        String codiceFiscale=request.getParameter("cf");
        String email=request.getParameter("eml");
        String fidelity=request.getParameter("fid");
        String via=request.getParameter("via");
        String numerocivico = request.getParameter("nciv");
        String password=request.getParameter("pw");



        Cliente cliente=new Cliente(Integer.parseInt(idCliente),nome,cognome,partitaIva,codiceFiscale, email,Integer.parseInt(fidelity),via,Integer.parseInt(numerocivico),password);

        d.salvaCliente(cliente);

        response.getWriter().append("utente registrato").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

and the JSP is

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registrazione</title>
</head>
<body>
<form action="/Registrazione" name="FormRegistrazione" method="get" >
Username:<input type="text" name="userid" value="">
<br>
Nome:<input type="text" name="nome" value="">
<br>
Cognome:<input type="text" name="cog" value="">
<br>
PIVA:<input type="text" name="piva" value="">
<br>
CF:<input type="text" name="cf" value="">
<br>
email:<input type="text" name="eml" value="">
<br>
fidelity:<input type="text" name="fid" value="">
<br>
via:<input type="text" name="via" value="">
<br>
civico:<input type="text" name="nciv" value="">
<br>
password:<input type="text" name="pw" value="">
<br>
<input type="submit" value="invia">
</form>
</body>
</html>
}*

but when i try to run the jsp from server i see this :

HTTP Status 500 – Internal Server Error


Type Exception Report

Message null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
it.unirc.twd.BuyeDrink.servlet.NuovoCliente.doGet(NuovoCliente.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.

I have a similar servlet and it run very good but also with "" value in the form. So I can't understand the problem with integer parseInt ... what's the problem?

String x = null;    
Integer.parseInt(x) // will rase the exception

Throws: NumberFormatException - if the string does not contain a parsable integer.

check oracle docs here

if i run this servlet :

@WebServlet({ "/AggiungiStudente", "/AggiungiStudente.jsp" })
public class AggiungiStudente extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public AggiungiStudente() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    StudenteDAO dao = new StudenteDAO();
    String matricola = request.getParameter("matricola");
    String nome = request.getParameter("nome");
    String cognome = request.getParameter("cognome");
    String password = request.getParameter("password");
    String idcdl = request.getParameter("idcdl");

    Studente s = new Studente(Integer.parseInt(matricola),nome,cognome,password,Integer.parseInt(idcdl));

    dao.salvaStudente(s);       

    response.sendRedirect("/VisualizzaStudente");

    response.getWriter().append("Utente aggiunto").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

with this JSP

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="/AggiungiStudente">
<table style="height: 400px; width: 598px;border: 1">
<tbody>
<tr>
<td style="width: 118px;">&nbsp;Matricola:</td>
<td style="width: 473px;">&nbsp;<input type="text" name="matricola" value=""></td>
</tr>
<tr>
<td style="width: 118px;">&nbsp;Nome:&nbsp;</td>
<td style="width: 473px;">&nbsp;<input type="text" name="nome" value=""></td>
</tr>
<tr>
<td style="width: 118px;">&nbsp;Cognome:</td>
<td style="width: 473px;">&nbsp;<input type="text" name="cognome" value=""></td>
</tr>
<tr>
<td style="width: 118px;">&nbsp;Password:</td>
<td style="width: 473px;">&nbsp;<input type="password" name="password" value=""></td>
</tr>
<tr>
<td style="width: 118px;">IdCdl:</td>
<td style="width: 473px;">&nbsp;<input type="text" name="idcdl" value=""></td>
</tr>
</tbody>
</table>


            <input type="submit" value="Aggiungi">
</form>         

</body>
</html>

when i click "Aggiungi" the value that i insert in the form are inserted in the database in oracle ... i want to make the same thing in another database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM