string 转换为 long 型。出现异常。
Long deptId = new Long(request.getParameter("deptId"));
exception java.lang.NumberFormatException: null
java.lang.Long.parseLong(Unknown Source)
java.lang.Long.<init>(Unknown Source)
求解
Long deptId = new Long(request.getParameter("deptId"));
exception java.lang.NumberFormatException: null
java.lang.Long.parseLong(Unknown Source)
java.lang.Long.<init>(Unknown Source)
求解
解决方案 »
- 使用request.getRequestDispatcher("detail.jsp").forward(request, response);出现空指针
- 如何在修改一条数据后获取某个字段的数据
- JSP包含Servlet出错?????
- ssh .struts-config.mex问题?
- struts中标签库位置问题
- ◎◎◎◎◎◎◎◎◎◎◎◎◎Lucene中文分词大探讨◎◎◎◎◎◎◎◎◎◎◎
- 有关xdoclet的,问问
- 帮忙看看,怎么不显示?
- 为什么Hibernate生产的sql 这么奇怪? select goods0_.id
- 一般事务 @Transactional 都开在service层吗
- 谢谢
- 为什么Eclipse启动Tomcat不成功,但手工启动Tomcat却可以呢?
NumberFormatException - if the String does not contain a parsable long.如果这个string不包含一个可以转换为long型的数据就会抛这个异常。
long.parseLong方法中是这么说的
An exception of type NumberFormatException is thrown if any of the following situations occurs: The first argument is null or is a string of length zero.
The radix is either smaller than java.lang.Character.MIN_RADIX or larger than java.lang.Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') provided that the string is longer than length 1.
The value represented by the string is not a value of type long.
就是在这些情况下会抛这个异常。
其次。我模拟出来重现出现这个异常的方法了。
如下:
public class test {
public static void main(String[] args) {
String b = null;
Long a = new Long(b);
System.out.println(a);
}
}
这个时候报的异常和楼主一致~~~因此。LZ请检查你的那个东西是不是null~~~~
request.getParameter("deptId")这个为NULL值,就是deptId所对应的值没有,一般是的JSP文本框内没有输入东西
还有转换最好用Long.parseLong(new String())
request.getParameter("deptId")获取的值为null,没有取到值
还有:在使用转换的时候 字符串应该是纯数字的,要不然也会出现类型转换异常的
建议使用。Long.parseLong()方法进行转换。。
java讨论群 167667040欢迎加入
public static void main(String[] args) {
String b = null;
if (null == b) {
System.out.println("传值为空,请确认是否有数据。");
} else {
Long a = new Long(b);
System.out.println(a);
}
}
}这样就可以了。先验证变量是否为空。不为空才进行转换。
为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
洗洗睡了。
为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
request.getParameter("deptId")应该是这个为空了
request.getParameter("deptId")是这个是Null所以才会报这个异常信息,检查一下是不是没有在请求中赋值
这种转换之前貌似都要加正则判断的好不好..
空的怎么能转换成Long?字母等等的非数字一般也不能转换成long啊..