String index out of range

String index out of range
StringIndexOutOfBoundsException

1 문제상황[ | ]

2016-06-07 20:47:27.268 ERROR 620 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: 38] with root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: 38
	at java.lang.String.substring(Unknown Source) ~[na:1.8.0_91]
	at com.example.springmoviez.MainController.actorsIndex(MainController.java:50) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]

2 확인[ | ]

str = str.substring(0, 38);

3 조치방법[ | ]

  • 방법 1: Math.min() 사용
str = str.substring(0, Math.min(str.length(), 38));
  • 방법 2: StringUtils.left() 사용
str = StringUtils.left(str, 38);

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}