안드로이드 개발

StringIndexOutOfBoundsException 해결 방법

피커 2023. 5. 4. 13:29
728x90
반응형

안드로이드 앱 개발중에 StringIndexOutOfBoundsException 에러를 발견하게 되는경우가 있습니다.

해당에러는 아주 단순한 에러라서 바로 해결이 가능합니다.

아래 에러 원인 및 해결 방법을 참고하세요.

 

1. 예제 코드 (오류 코드)

  -. 아래 예제 코드에서 파란색 마킹된 부분이 에러의 원인입니다.

     기존 munja String은 3글자이지만, substring으로 4글자를 가져오려고 합니다.

     당연히 에러가 발생하게 됩니다.

2. 에러 로그

3. 해결 방법

   -. 아주 간단히 substring을 할때 문자열 길이 값인 3까지만 하면 해결됩니다.

      아래 파란색 마킹된 부분을 참고하세요.

 

 

*. 에러 로그 원문

FATAL EXCEPTION: main
Process: com.picker.javaexception, PID: 19856
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.picker.javaexception/com.picker.javaexception.MainActivity}: java.lang.StringIndexOutOfBoundsException: length=3; index=4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at co m.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at co m.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
Caused by: java.lang.StringIndexOutOfBoundsException: length=3; index=4
at java.lang.String.substring(String.java:2060)
at com.picker.javaexception.MainActivity.plusNumber(MainActivity.java:21)
at com.picker.javaexception.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7839) 
at java.lang.reflect.Method.invoke(Native Method) 
at co m.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)  
at co m.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)  

*. 관련 exception 원문

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.

Summary

 
 

Public constructors

StringIndexOutOfBoundsException

Added in API level 1
 
Constructs a StringIndexOutOfBoundsException with no detail message.

StringIndexOutOfBoundsException

Added in API level 1
 
Constructs a StringIndexOutOfBoundsException with the specified detail message.
 

StringIndexOutOfBoundsException

Added in API level 1
 
Constructs a new StringIndexOutOfBoundsException class with an argument indicating the illegal index.
The index is included in this exception's detail message. The exact presentation format of the detail message is unspecified.
 
반응형