request.getParameter 得到 java.lang.NumberFormatException: 空
request.getParameter getting java.lang.NumberFormatException: null
我有一个下拉菜单,它从 SQL table 中提取数据。一切正常,我可以 select 值并提交表单。但是,当我从 String 转换值时,我的 scriptlet 给出了 NULL 异常。我做错了什么?
testCars.jsp
<form method=post action=“testCars.jsp">
<div class="cell">
Select CarID
<div class="input-control select mutiple full-size">
<select id=“carID” name=“carID” onchange="listCarIDFromDatabase();”>
<option selected disabled>--SELECT--</option>
<%
ArrayList<Integer> result = carDAO.getCarID(Integer.parseInt(id));
for (int r: result){
out.println("<option value='" + r + "'>" + r + "</option>");
request.setAttribute("r", r);
}
%>
</select>
</div>
</div>
输入 returncarID 根据 AJAX success onchange="listCarIDFromDatabase();
填充
<button class="button primary" type="submit">Generate</button></form>
<input name=“returncarID” id=“returncarID”>
<%
//Scriptlet gets the value of the dropdown in next line, but when I convert to Int it is NULL
String y = request.getParameter(“returncarID”);
out.println(y); <——Prints out value here
int x = Integer.parseInt(y);
out.println(x); <———Null Exception here
%>
如果输出为 900056
并且您在编写时得到 java.lang.NumberFormatException
:
int x = Integer.parseInt(y);
out.println(x);
我认为问题出在初始 String
它可能有尾随或前导空格,为避免此问题,请在解析之前将 .trim()
与您的 String
一起使用:
int x = Integer.parseInt(y.trim());
out.println(x);
我有一个下拉菜单,它从 SQL table 中提取数据。一切正常,我可以 select 值并提交表单。但是,当我从 String 转换值时,我的 scriptlet 给出了 NULL 异常。我做错了什么?
testCars.jsp
<form method=post action=“testCars.jsp">
<div class="cell">
Select CarID
<div class="input-control select mutiple full-size">
<select id=“carID” name=“carID” onchange="listCarIDFromDatabase();”>
<option selected disabled>--SELECT--</option>
<%
ArrayList<Integer> result = carDAO.getCarID(Integer.parseInt(id));
for (int r: result){
out.println("<option value='" + r + "'>" + r + "</option>");
request.setAttribute("r", r);
}
%>
</select>
</div>
</div>
输入 returncarID 根据 AJAX success onchange="listCarIDFromDatabase();
填充 <button class="button primary" type="submit">Generate</button></form>
<input name=“returncarID” id=“returncarID”>
<%
//Scriptlet gets the value of the dropdown in next line, but when I convert to Int it is NULL
String y = request.getParameter(“returncarID”);
out.println(y); <——Prints out value here
int x = Integer.parseInt(y);
out.println(x); <———Null Exception here
%>
如果输出为 900056
并且您在编写时得到 java.lang.NumberFormatException
:
int x = Integer.parseInt(y);
out.println(x);
我认为问题出在初始 String
它可能有尾随或前导空格,为避免此问题,请在解析之前将 .trim()
与您的 String
一起使用:
int x = Integer.parseInt(y.trim());
out.println(x);