【问题标题】:request.getParameter getting java.lang.NumberFormatException: null [duplicate]request.getParameter 获取 java.lang.NumberFormatException:null [重复]
【发布时间】:2017-09-30 00:14:12
【问题描述】:

我有一个从 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>

根据 AJAX 成功填充输入 returncarID 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


%>

【问题讨论】:

  • y 的值是数字还是其他字符串?
  • 在这里打印出值,这个输出是什么?
  • 值打印:900056

标签: java string jsp int numberformatexception


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2012-05-23
    • 1970-01-01
    • 2016-11-02
    • 2015-04-12
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    相关资源
    最近更新 更多