0

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. but when i am inserting "123456789111111" it throws NumberFormatException

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

Here 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)
Ashish Shah
  • 33
  • 3
  • 12
  • "123456789111111" is more than the maximum value of the java integer (max = 2147483647 and min = -2147483648) – julianstark999 Feb 19 '16 at 10:21
  • well, you are beyond the MAX value for an `Integer`. Use `Long` if you expect your values to be that big – jmcg Feb 19 '16 at 10:49

6 Answers6

3

If you look at the stacktrace you see 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.

If you check the documentation from the oracle website:

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. 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. Use the Integer class to use int data type as an unsigned integer. 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.

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • means there is no issue with int(100) ? what is the issue why you have suggest Long. Thank you for your time. @maythesource.com – Ashish Shah Feb 19 '16 at 10:23
  • @Ashish Shah Each data type has a different range based on the number of bits used. The number you entered requires more bits to store. Checking the documentation on the oracle site Long is the type that fits your number. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1 *(-9,22337E+18 to 9,22337E+18) – Menelaos Feb 19 '16 at 10:28
  • Also have a look at the following, it's good information to have about the difference between Java and C/C++. Java types are standard since everything runs on a JVM. For C/C++ the types change depending on the architecture of the machine ( http://stackoverflow.com/questions/15004944/max-value-of-integer?lq=1) . – Menelaos Feb 19 '16 at 10:31
  • issue solved to change to Long. and also i have change BIGINT of my data table field. Thanks @maythesource.com – Ashish Shah Feb 19 '16 at 10:33
  • It should be enough to change to Long. BigInteger is for much larger numbers but you don't need it. :-)\ – Menelaos Feb 19 '16 at 10:34
3

Integer number out of range. change your type to Long.

Integer values should be in between

A constant holding the maximum value an int can have, 2 ^31-1. i.e 2147483647

A constant holding the minimum value an int can have, -2^31.i.e 2147483648

It is always recommended to use Long for Ids because It will take 64 bits i.e

2^ 63-1 - -2^63

Pragnani
  • 20,075
  • 6
  • 49
  • 74
1

Number out of range. "123456789111111" is a long number.

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

Community
  • 1
  • 1
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
0

The Number 123,456,789,111,111 is above Integer.MAX_INT, which is 2,147,483,647 (2^31-1), you want to be using a Long rather than an Integer as a Long can be up to 2^63-1 (9,223,372,036,854,775,807) if you are expecting numbes that large.

Justinw
  • 631
  • 10
  • 18
  • Hello every one..Thanks to allz..done with change to Long Type. and also i have changed my data field type in BIGINT. Thanks a lot. – Ashish Shah Feb 19 '16 at 10:31
0

Use Long instead of Integer for IDENTITY fields. Decimal for currency operations

svichkar
  • 1,806
  • 1
  • 17
  • 23
0

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.

The signed long has a minimum value of -2^63 and a maximum value of (2^63) - 1.

Int data type is a 32-bit signed two's complement integer. Minimum value is - 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31 -1)

White Bullet
  • 205
  • 2
  • 11