类型转换抛异常~!!!

pinotao 2011-10-09 11:29:09
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)
求解
...全文
169 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
瑞恩_张 2011-10-10
  • 打赏
  • 举报
回复
public class test {
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);
}
}
}


这样就可以了。先验证变量是否为空。不为空才进行转换。
为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
洗洗睡了。
tao_tao 2011-10-10
  • 打赏
  • 举报
回复
你看异常提示:第二行
request.getParameter("deptId")获取的值为null,没有取到值
还有:在使用转换的时候 字符串应该是纯数字的,要不然也会出现类型转换异常的
建议使用。Long.parseLong()方法进行转换。。
java讨论群 167667040欢迎加入


pinotao 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuhongdong2010 的回复:]

java.lang.Long.parseLong(Unknown Source)
request.getParameter("deptId")这个为NULL值,就是deptId所对应的值没有,一般是的JSP文本框内没有输入东西
还有转换最好用Long.parseLong(new String())
[/Quote]
恩,谢谢好得建议。 JSP是没有写内容,开始。那用try catch处理还是赋初值,按我的写法,怎么赋初值。
pinotao 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 z6683227 的回复:]

首先 - -# 原谅我连续回了三次这个帖子。
其次。我模拟出来重现出现这个异常的方法了。
如下:
Java code
public class test {
public static void main(String[] args) {
String b = null;
Long a = new Long(b);
System.o……
[/Quote]
辛苦了,变量只定义未赋值~~ 赋初值是吗?怎样赋初值不报错,按我的写法。
linsn007 2011-10-10
  • 打赏
  • 举报
回复
晕....
这种转换之前貌似都要加正则判断的好不好..
空的怎么能转换成Long?字母等等的非数字一般也不能转换成long啊..
duan 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 pinotao 的回复:]
string 转换为 long 型。出现异常。


Java code


Long deptId = new Long(request.getParameter("deptId"));



exception

java.lang.NumberFormatException: null
java.lang.Long.parseLong(Unknown Source)……
[/Quote]
LZ,先捕捉一下异常,try{......}catcht(Exception e){....}方便能捕捉的是哪一行抛的异常信息,利于定位问题点出在哪!
request.getParameter("deptId")是这个是Null所以才会报这个异常信息,检查一下是不是没有在请求中赋值
scrack 2011-10-10
  • 打赏
  • 举报
回复
先验证变量是否为空。不为空才进行转换。
为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
request.getParameter("deptId")应该是这个为空了
liuhongdong2010 2011-10-09
  • 打赏
  • 举报
回复
java.lang.Long.parseLong(Unknown Source)
request.getParameter("deptId")这个为NULL值,就是deptId所对应的值没有,一般是的JSP文本框内没有输入东西
还有转换最好用Long.parseLong(new String())
瑞恩_张 2011-10-09
  • 打赏
  • 举报
回复
首先 - -# 原谅我连续回了三次这个帖子。
其次。我模拟出来重现出现这个异常的方法了。
如下:
public class test {
public static void main(String[] args) {
String b = null;
Long a = new Long(b);
System.out.println(a);
}
}

这个时候报的异常和楼主一致~~~

因此。LZ请检查你的那个东西是不是null~~~~
pinotao 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 z6683227 的回复:]

是不是说你request里的parameter:deptId是空的。
[/Quote]
恩,数据库还没写内容进去`
瑞恩_张 2011-10-09
  • 打赏
  • 举报
回复
Throws:
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.
就是在这些情况下会抛这个异常。
瑞恩_张 2011-10-09
  • 打赏
  • 举报
回复
是不是说你request里的parameter:deptId是空的。

67,512

社区成员

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

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