request.getParameter获取java.lang.NumberFormatException:null

时间:2017-05-02 12:54:05

标签: java string jsp int numberformatexception

我有一个下拉菜单,用于从SQL表中提取数据。一切正常,我可以选择价值并提交表格。但是,当我从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成功onchange =&#34; 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


%>

1 个答案:

答案 0 :(得分:1)

如果输出为900056,并且您在撰写时获得java.lang.NumberFormatException

int x = Integer.parseInt(y);
out.println(x);

我认为问题在于初始String它可能有一个尾随或前导空格,以避免此问题在解析之前使用.trim() String

int x = Integer.parseInt(y.trim());
out.println(x);