java.lang.NumberFormatException:带Integer.Parseint()的null

时间:2018-08-27 11:00:26

标签: java oracle jsp servlets

**当我尝试在服务器上运行我的JSP时遇到此问题... servlet是:

@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);
    }

而JSP是

<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>
}*

但是当我尝试从服务器运行jsp时,我会看到:

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.

我有一个类似的servlet,它运行得很好,但表单中也带有“”值。 所以我无法理解整数parseInt的问题...是什么问题?

2 个答案:

答案 0 :(得分:0)

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

抛出:   NumberFormatException-如果字符串不包含可分析的整数。

检查oracle文档here

答案 1 :(得分:0)

如果我运行此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);
}

}

使用此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>

当我单击“ Aggiungi”时,我在表单中插入的值将插入到oracle的数据库中……我想在另一个数据库中做同样的事情