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)
求解

解决方案 »

  1.   

    是不是说你request里的parameter:deptId是空的。
      

  2.   

    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.
    就是在这些情况下会抛这个异常。
      

  3.   

    首先 - -# 原谅我连续回了三次这个帖子。
    其次。我模拟出来重现出现这个异常的方法了。
    如下:
    public class test {
    public static void main(String[] args) {
    String b = null;
    Long a = new Long(b);
    System.out.println(a);
    }
    }
    这个时候报的异常和楼主一致~~~因此。LZ请检查你的那个东西是不是null~~~~
      

  4.   

    java.lang.Long.parseLong(Unknown Source)
    request.getParameter("deptId")这个为NULL值,就是deptId所对应的值没有,一般是的JSP文本框内没有输入东西
    还有转换最好用Long.parseLong(new String())
      

  5.   

    恩,谢谢好得建议。 JSP是没有写内容,开始。那用try catch处理还是赋初值,按我的写法,怎么赋初值。
      

  6.   

    你看异常提示:第二行
    request.getParameter("deptId")获取的值为null,没有取到值
    还有:在使用转换的时候 字符串应该是纯数字的,要不然也会出现类型转换异常的
    建议使用。Long.parseLong()方法进行转换。。
    java讨论群 167667040欢迎加入
      

  7.   

    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);
    }
    }
    }这样就可以了。先验证变量是否为空。不为空才进行转换。
    为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
    洗洗睡了。
      

  8.   

    先验证变量是否为空。不为空才进行转换。
    为空的话你可以选择控制台打印日志。或者页面弹出对话框提示等等等等。
    request.getParameter("deptId")应该是这个为空了 
      

  9.   

    LZ,先捕捉一下异常,try{......}catcht(Exception e){....}方便能捕捉的是哪一行抛的异常信息,利于定位问题点出在哪!
    request.getParameter("deptId")是这个是Null所以才会报这个异常信息,检查一下是不是没有在请求中赋值
      

  10.   

    晕....
    这种转换之前貌似都要加正则判断的好不好..
    空的怎么能转换成Long?字母等等的非数字一般也不能转换成long啊..