java.lang.NumberFormatException:带整数的null.Parseint()
*当我试图在服务器上运行我的JSP时,我遇到了这个问题。。。 servlet是:java.lang.NumberFormatException:带整数的null.Parseint(),java,oracle,jsp,servlets,Java,Oracle,Jsp,Servlets,*当我试图在服务器上运行我的JSP时,我遇到了这个问题。。。 servlet是: @WebServlet({"/Registrazione" , "/Registrazione.jsp"}) public class Registrazione extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet()
@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>
}*
我有一个类似的servlet,它运行得非常好,但表单中也有“”值。
所以我不能理解整数parseInt的问题。。。有什么问题吗
String x = null;
Integer.parseInt(x) // will rase the exception
抛出:
NumberFormatException-如果字符串不包含可解析整数
如果我运行此servlet,请检查oracle文档:
@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;"> Matricola:</td>
<td style="width: 473px;"> <input type="text" name="matricola" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Nome: </td>
<td style="width: 473px;"> <input type="text" name="nome" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Cognome:</td>
<td style="width: 473px;"> <input type="text" name="cognome" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Password:</td>
<td style="width: 473px;"> <input type="password" name="password" value=""></td>
</tr>
<tr>
<td style="width: 118px;">IdCdl:</td>
<td style="width: 473px;"> <input type="text" name="idcdl" value=""></td>
</tr>
</tbody>
</table>
<input type="submit" value="Aggiungi">
</form>
</body>
</html>
在此处插入标题
Matricola:
诺姆:
Cognome:
密码:
IdCdl:
当我单击“Aggiungi”时,我在表单中插入的值将插入到oracle的数据库中。。。我想在另一个数据库中做同样的事情您需要检查试图解析为整数的字符串值。例如,不可能解析空字符串。我有一个类似的servlet,它运行得非常好,但在表单中也有“value”。如果它只是一个集合,当然可以。如果它必须传递一个整数.parseInt(),那么它也不会工作,除非您以某种方式捕捉到异常欢迎使用堆栈溢出。这几乎是要求我们阅读和理解的代码。你可以吗?也要问,因为在这样做的过程中,你有很好的机会解决你自己的问题。
<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;"> Matricola:</td>
<td style="width: 473px;"> <input type="text" name="matricola" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Nome: </td>
<td style="width: 473px;"> <input type="text" name="nome" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Cognome:</td>
<td style="width: 473px;"> <input type="text" name="cognome" value=""></td>
</tr>
<tr>
<td style="width: 118px;"> Password:</td>
<td style="width: 473px;"> <input type="password" name="password" value=""></td>
</tr>
<tr>
<td style="width: 118px;">IdCdl:</td>
<td style="width: 473px;"> <input type="text" name="idcdl" value=""></td>
</tr>
</tbody>
</table>
<input type="submit" value="Aggiungi">
</form>
</body>
</html>