Integer.parseInt问题?

hellojava1 2006-04-27 04:00:05
同一jsp页面代码
<form id="select" name="select" method="post" action="">
<label>关键字:
<input name="select" type="text" id="select" />
</label>
<label>条件:
<select name="select2" size="1">
<option value="xh">学号</option>
</select>
</label>
<label>
<input type="submit" name="Submit" value="查询" />
</label>
</form>

<%int xuehao = 0;
xuehao = Integer.parseInt(request.getParameter("select"));%>

请求本页面时,出现异常:
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:415)
如何解决?
...全文
457 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hellojava1 2006-04-27
  • 打赏
  • 举报
回复
try/catch了一下。解决了。谢谢楼上的热心哥哥~~~
sole_lodestar 2006-04-27
  • 打赏
  • 举报
回复

/**
* 无异常的parseInt方法
* @param s
* @return
*/
public static int parseInt(String s) {
if (s == null) {
return 0;
}
try {
return Integer.parseInt(s);
}
catch (NumberFormatException nfe) {
return 0;
}
}
Marcus-Z 2006-04-27
  • 打赏
  • 举报
回复
<%int xuehao = 0;
xuehao = Integer.parseInt(request.getParameter("select"));%>

若是你前台页面的select控件的值为null或不为数字的值比如:ssd什么的
你的xuehao = Integer.parseInt(request.getParameter("select"))当然就出现异常了,
所以要保证request.getParameter("select")为可转成int的值,
sole_lodestar 2006-04-27
  • 打赏
  • 举报
回复
处理值不是数字类型,可能为null或字符型,加一下try/catch

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧