首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

java.lang.NumberFormatException: null

2011-11-13 
类型转换抛异常~!!!string 转换为 long 型。出现异常。Java codeLong deptId new Long(request.getParamet

类型转换抛异常~!!!
string 转换为 long 型。出现异常。

Java code
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里的parameter:deptId是空的。
[解决办法]
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.
就是在这些情况下会抛这个异常。
[解决办法]
首先 - -# 原谅我连续回了三次这个帖子。
其次。我模拟出来重现出现这个异常的方法了。
如下:
Java code
public class test {    public static void main(String[] args) {        String b = null;        Long a = new Long(b);        System.out.println(a);    }}
[解决办法]
java.lang.Long.parseLong(Unknown Source)
request.getParameter("deptId")这个为NULL值,就是deptId所对应的值没有,一般是的JSP文本框内没有输入东西
还有转换最好用Long.parseLong(new String())
[解决办法]
你看异常提示:第二行
request.getParameter("deptId")获取的值为null,没有取到值
还有:在使用转换的时候 字符串应该是纯数字的,要不然也会出现类型转换异常的
建议使用。Long.parseLong()方法进行转换。。
java讨论群 167667040欢迎加入



[解决办法]
Java code
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);        }    }}
[解决办法]
先验证变量是否为空。不为空才进行转换。
为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
request.getParameter("deptId")应该是这个为空了 

[解决办法]
探讨
string 转换为 long 型。出现异常。


Java code


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



exception

java.lang.NumberFormatException: null
java.lang.Long.parseLong(Unknown Source)……

热点排行