简体   繁体   English

如何解决Java中的NumberFormatException?

[英]How to solve NumberFormatException in Java?

Table Field 表格栏位

Field: Bank_Account_No
Type : int(100)

This is my 'account' model with setter/getter method 这是我的带有设置器/获取器方法的“帐户”模型

@Entity
@Table(name = "account")
public class Account {

@Column(name = "Pharmacy_Account_No")
private Integer pharmacyAccountNo;

public Integer getPharmacyAccountNo() {
    return pharmacyAccountNo;
}

public void setPharmacyAccountNo(Integer pharmacyAccountNo) {
    this.pharmacyAccountNo = pharmacyAccountNo;
   }
}

and if i am inserting "1234567891" it will added successfully. 如果我插入“ 1234567891”,它将成功添加。 but when i am inserting "123456789111111" it throws NumberFormatException 但是当我插入“ 123456789111111”时,它将引发NumberFormatException

Inserting Code:
pharmacyAccount.setPharmacyAccountNo(request.getParameter("pharmacyAccountNo"). != null
                        && !request.getParameter("pharmacyAccountNo").equals("")
                                ? Integer.valueOf(request.getParameter("pharmacyAccountNo") : 0);

Here StackTrach : 在这里StackTrach:

java.lang.NumberFormatException: For input string: "123456789111111"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.pharmacy.ServiceImpl.PharmacyServiceImpl.savePharmacyAccount(PharmacyServiceImpl.java:977)
at com.pharmacy.ServiceImpl.PharmacyServiceImpl.savePharmacy(PharmacyServiceImpl.java:838)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy29.savePharmacy(Unknown Source)
at com.pharmacy.Controller.PharmacyController.savePharmacy(PharmacyController.java:232)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:99)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:188)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

If you look at the stacktrace you see java.lang.Integer.parseInt 如果查看stacktrace,则会看到java.lang.Integer.parseInt

The number you are providing is too large out of the range of an Integer. 您提供的数字太大,超出了整数范围。 You should try using a Long . 您应该尝试使用Long

If you check the documentation from the oracle website: 如果您从oracle网站检查文档:

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. int:默认情况下,int数据类型为32位带符号的二进制补码整数,最小值为-231,最大值为231-1。 In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. 在Java SE 8和更高版本中,可以使用int数据类型表示无符号的32位整数,其最小值为0,最大值为232-1。 Use the Integer class to use int data type as an unsigned integer. 使用Integer类可将int数据类型用作无符号整数。 See the section The Number Classes for more information. 有关更多信息,请参见“数字类”部分。 Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers. 静态方法(如compareUnsigned,divideUnsigned等)已添加到Integer类中,以支持无符号整数的算术运算。

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 资料来源: https : //docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Integer number out of range. Integer超出范围。 change your type to Long . 将您的类型更改为Long

Integer values should be in between 整数值应介于

A constant holding the maximum value an int can have, 2 ^31-1 . 保持int可以具有的最大值的常数2 ^31-1 ie 2147483647 2147483647

A constant holding the minimum value an int can have, -2^31 .ie 2147483648 保持int可以具有的最小值的常量, -2^31 .ie 2147483648

It is always recommended to use Long for Ids because It will take 64 bits ie 始终建议使用Long for Ids,因为它将占用64位,即

2^ 63-1 - -2^63 2^ 63-1 -2^63

Number out of range. 号码超出范围。 "123456789111111" is a long number. "123456789111111"是长数字。

What are the ranges: 范围是多少:

              width                     minimum                         maximum
signed    8 bit                        -128                            +127
signed   16 bit                     -32 768                         +32 767
signed   32 bit              -2 147 483 648                  +2 147 483 647
signed   64 bit  -9 223 372 036 854 775 808      +9 223 372 036 854 775 807
unsigned  8 bit                           0                            +255
unsigned 16 bit                           0                         +65 535
unsigned 32 bit                           0                  +4 294 967 295
unsigned 64 bit                           0     +18 446 744 073 709 551 615

Reference . 参考 Check Integer for descriptions 检查整数以获取描述

数字123,456,789,111,111在Integer.MAX_INT之上,该数字为2,147,483,647(2 ^ 31-1),您要使用Long而不是Integer,因为如果您期望,Long最多可以是2 ^ 63-1 (9,223,372,036,854,775,807)麻木那么大。

Use Long instead of Integer for IDENTITY fields. 对于IDENTITY字段,请使用Long而不是Integer Decimal for currency operations 货币操作的小数

As mentioned above, it is preferable to use a Long instead of an Integer object, as this would allow your application to handle numbers with large sizes. 如上所述,最好使用Long而不是Integer对象,因为这将允许您的应用程序处理大尺寸的数字。

The signed long has a minimum value of -2^63 and a maximum value of (2^63) - 1. 带符号的long的最小值为-2 ^ 63 ,最大值为(2 ^ 63)-1。

Int data type is a 32-bit signed two's complement integer. Int数据类型是32位带符号的二进制补码整数。 Minimum value is - 2,147,483,648. 最小值是-2,147,483,648。 (-2^31) Maximum value is 2,147,483,647(inclusive). (-2 ^ 31)最大值为2,147,483,647(含)。 (2^31 -1) (2 ^ 31 -1)

暂无
暂无

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

相关问题 如何在回文中解决此java.lang.NumberFormatException? - How to solve this java.lang.NumberFormatException in palindrome? java - 当字符串以零结尾时,如何解决NumberFormatException? - How can I solve NumberFormatException when string endswith zero in Java? 如何解决 java.lang.NumberFormatException: empty String - How to solve java.lang.NumberFormatException: empty String 如何解决 java.lang.NumberFormatException:对于输入字符串:“396.00” - How to solve java.lang.NumberFormatException: For input string: "396.00" 如何在Java中修复NumberFormatException? - How to fix NumberFormatException in Java? 如何解决“java.lang.NumberFormatException: s == null at java.lang.Integer.parseInt” - How to solve “java.lang.NumberFormatException: s == null at java.lang.Integer.parseInt” 如何解决java.lang.NumberFormatException:代码中的int无效:“”错误? - how do I solve java.lang.NumberFormatException: Invalid int: “” error in my code? 如何解决 spring 启动反馈邮件发送器中的此 NumberFormatException - How to solve this NumberFormatException in spring boot feedback mailsender 如何解决这个 java.lang.NumberFormatException: For input string: "" 基于我的代码? - How to solve this java.lang.NumberFormatException: For input string: "" based on my code? 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:空字符串……但如何解决 - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: empty String… but how to solve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM