Long.parseLong给出了java.lang.NumberFormatException

时间:2014-12-22 06:53:31

标签: java

所以我尝试了很多东西,但是我无法解决这个问题。我的id类中的Service数据类型为long。我已尝试将serviceAuth转换为long,但会抛出java.lang.NumberFormatException。我该如何解决这个问题?

String[] serviceList = getUser.serviceList.split(",");

for(String serviceAuth: serviceList) {
    Long temp = Long.parseLong(serviceAuth.toString());
    Criteria ctr = sessionFactory.getCurrentSession().createCriteria(Service.class)
                        .add(Restrictions.eq("id",temp));
}

serviceList看起来像5,18,19。它是从csv文件加载的。

当我在for循环中打印值时,它看起来像这样:

1
2
14
15

错误报告:

 java.lang.NumberFormatException: For input string: ""
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    java.lang.Long.parseLong(Long.java:601)
    java.lang.Long.parseLong(Long.java:631)
    org.x.y.gateway.MainController.getUsers(MainController.java:1433)
    org.x.y.gateway.MainController$$FastClassBySpringCGLIB$$6e5db2d9.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    org.x.y.gateway.MainController$$EnhancerBySpringCGLIB$$149bedb6.getUsers(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.x.y.gateway.CrossOriginRequest.doFilter(CrossOriginRequest.java:18)

4 个答案:

答案 0 :(得分:4)

您的问题是split()创建的其中一个字符串为空或包含空格,例如。

package de.scrum_master.app;

public class Application {
    public static void main(String[] args) {
        String[] serviceList = ",15,18,19".split(",");
        for (String serviceAuth : serviceList) {
            Long temp = Long.parseLong(serviceAuth.toString());
            System.out.println(temp);
        }
    }
}
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at de.scrum_master.app.Application.main(Application.java:7)

更新:如何解决问题

你应该做两件事:

  • 使分割正则表达式对前导和尾随空格更加稳定。
  • 在循环中跳过""的值,它仍然可以出现在数组的开头。
package de.scrum_master.app;

public class Application {
    public static void main(String[] args) {
        String[] serviceList = " , 15 , 18 , 19, ".split("[\\s,]+");
        for (String serviceAuth : serviceList) {
            if ("".equals(serviceAuth))
                continue;
            Long temp = Long.parseLong(serviceAuth.toString());
            System.out.println(temp);
        }
    }
}
15
18
19

答案 1 :(得分:1)

您尝试解析的字符串长度不正确。该异常还会显示哪个字符串导致了问题。

答案 2 :(得分:1)

输入数据有问题。 ""表示字符串为空。 Long无法解析空字符串。

答案 3 :(得分:1)

String为空(输入字符串:“”),尝试对其进行硬编码并打印结果或在此处提供输入