简体   繁体   English

Long.parseLong给出了java.lang.NumberFormatException

[英]Long.parseLong gives java.lang.NumberFormatException

So I've tried a whole bunch of things, but I'm unable to get around this. 所以我尝试了很多东西,但是我无法解决这个问题。 id in my Service class is of data type long . 我的Service类中的id数据类型为long I've tried to convert serviceAuth to long but it throws a java.lang.NumberFormatException . 我尝试将serviceAuth转换为long,但是它抛出java.lang.NumberFormatException How do I fix this? 我该如何解决?

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 looks like this 5,18,19 . serviceList看起来像这个5,18,19 It is loaded from a csv file. 它是从csv文件加载的。

When I print the values in the for loop, it looks like this: 当我在for循环中打印值时,它看起来像这样:

1
2
14
15

Error report: 错误报告:

 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)

Your problem is that one of the strings created by split() is empty or contains whitespace, eg 您的问题是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)

Update: How to fix the problem 更新:如何解决问题

You should do two things: 您应该做两件事:

  • Make the split regex more stable against leading and trailing spaces. 使分割正则表达式对前导和尾随空格更稳定。
  • Skip values of "" in the loop which can still occur at the beginning of the array. 在循环中跳过""值仍然可以在数组的开头出现。
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

The string you are trying to parse is not a correct long. 您尝试解析的字符串不是正确的long。 The exception also shows you which string caused the problem. 该异常还会显示哪个字符串导致了问题。

Something is wrong with your input data. 输入数据有问题。 "" means the string is empty. “”表示字符串为空。 Long cannot parse an empty string. Long无法解析空字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM