java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Spring JIRA | Michael Simons | 3 years ago
tip
Do you find the tips below useful? Click on the to mark them and say thanks to rafael . Or join the community to write better ones.
  1. 0

    The performance improvement of SPR-11304 is nice, but has one flaw. If a field name is "" (which is the case of a class level validator like the following (http://stackoverflow.com/a/2155576) {code} import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.apache.commons.beanutils.PropertyUtils; public class FieldsMatchValidator implements ConstraintValidator<FieldsMatch, Object> { private String firstFieldName; private String secondFieldName; @Override public void initialize(final FieldsMatch constraintAnnotation) { firstFieldName = constraintAnnotation.first(); secondFieldName = constraintAnnotation.second(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { boolean rv = false; try { final Object firstObj = PropertyUtils.getProperty(value, firstFieldName); final Object secondObj = PropertyUtils.getProperty(value, secondFieldName); rv = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj); } catch (final Exception ignore) { } return rv; } } {code} added to a class like so {code} @FieldsMatch(first = "newPassword", second = "newPasswordConfirmation") public class UpdateUserPasswordCmd { @NotBlank private String oldPassword; @NotBlank @Length(min=6, max=40) private String newPassword; @NotBlank @Length(min=6, max=40) private String newPasswordConfirmation; ... {code} it breaks with: {noformat} java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at org.springframework.validation.AbstractErrors.isMatchingFieldError(AbstractErrors.java:214) at org.springframework.validation.AbstractBindingResult.getFieldError(AbstractBindingResult.java:207) at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:115) at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102) at org.springframework.validation.DataBinder.validate(DataBinder.java:772) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:159) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:107) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) {noformat} I have the impression it happens only in combination with other validations and with the default violations in place. I'm adding now {code} context .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()) .addNode(secondFieldName) .addConstraintViolation() .disableDefaultConstraintViolation(); ; {code} to my validator which is better on it's one, but i think the slightly different semantics should be fixed. Take care and thank you.

    Spring JIRA | 3 years ago | Michael Simons
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  2. 0

    The performance improvement of SPR-11304 is nice, but has one flaw. If a field name is "" (which is the case of a class level validator like the following (http://stackoverflow.com/a/2155576) {code} import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.apache.commons.beanutils.PropertyUtils; public class FieldsMatchValidator implements ConstraintValidator<FieldsMatch, Object> { private String firstFieldName; private String secondFieldName; @Override public void initialize(final FieldsMatch constraintAnnotation) { firstFieldName = constraintAnnotation.first(); secondFieldName = constraintAnnotation.second(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { boolean rv = false; try { final Object firstObj = PropertyUtils.getProperty(value, firstFieldName); final Object secondObj = PropertyUtils.getProperty(value, secondFieldName); rv = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj); } catch (final Exception ignore) { } return rv; } } {code} added to a class like so {code} @FieldsMatch(first = "newPassword", second = "newPasswordConfirmation") public class UpdateUserPasswordCmd { @NotBlank private String oldPassword; @NotBlank @Length(min=6, max=40) private String newPassword; @NotBlank @Length(min=6, max=40) private String newPasswordConfirmation; ... {code} it breaks with: {noformat} java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at org.springframework.validation.AbstractErrors.isMatchingFieldError(AbstractErrors.java:214) at org.springframework.validation.AbstractBindingResult.getFieldError(AbstractBindingResult.java:207) at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:115) at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102) at org.springframework.validation.DataBinder.validate(DataBinder.java:772) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:159) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:107) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) {noformat} I have the impression it happens only in combination with other validations and with the default violations in place. I'm adding now {code} context .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()) .addNode(secondFieldName) .addConstraintViolation() .disableDefaultConstraintViolation(); ; {code} to my validator which is better on it's one, but i think the slightly different semantics should be fixed. Take care and thank you.

    Spring JIRA | 3 years ago | Michael Simons
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  3. 0

    BindingResult issue with custom ConstraintValidator

    Stack Overflow | 2 years ago | Tom H
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0
    samebug tip
    Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.
  6. 0

    error retrieving sequence

    GitHub | 2 years ago | nathandunn
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  1. linxiaolong 1 times, last 1 month ago
  2. jokester 1 times, last 2 months ago
  3. tyson925 1 times, last 2 months ago
  4. Torasque 1 times, last 3 months ago
  5. Hronom 32257 times, last 3 months ago
11 more registered users
18 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.StringIndexOutOfBoundsException

    String index out of range: -1

    at java.lang.String.charAt()
  2. Java RT
    String.charAt
    1. java.lang.String.charAt(String.java:658)
    1 frame
  3. Spring Context
    DataBinder.validate
    1. org.springframework.validation.AbstractErrors.isMatchingFieldError(AbstractErrors.java:214)
    2. org.springframework.validation.AbstractBindingResult.getFieldError(AbstractBindingResult.java:207)
    3. org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:115)
    4. org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102)
    5. org.springframework.validation.DataBinder.validate(DataBinder.java:772)
    5 frames
  4. Spring
    InvocableHandlerMethod.invokeForRequest
    1. org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:159)
    2. org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:107)
    3. org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    4. org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    5. org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    5 frames
  5. Spring MVC
    FrameworkServlet.doPost
    1. org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    2. org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    3. org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    4. org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    5. org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    6. org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    7. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
    8. org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
    8 frames
  6. JavaServlet
    HttpServlet.service
    1. javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    1 frame
  7. Spring MVC
    FrameworkServlet.service
    1. org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    1 frame