-5

in my little app I want to display something from my String.xml I normaly use this to do this:

public void putText() {
    String[] text1 = getResources().getStringArray(R.array.text1);
    String[] text2 = getResources().getStringArray(R.array.text2);


    if(selecter >= MAX) {
        selecter = 0;
    }
    else {
        selecter++;
    }

    // Texte anzeigen und Shown table auf true setzen.
    // normally selecter instead of 6
    txt_text1.setText(text1[6]);
    txt_text2.setText(text2[6]);
}

but when 'selecter' is between 6 and 9(nine is the MAX variable) the app crashes with this error:

FATAL EXCEPTION: main
                                                   Process: com.example.alex.wouldyoupressthebutton, PID: 22844
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.wouldyoupressthebutton/com.example.alex.wouldyoupressthebutton.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6316)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                                                    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
                                                       at com.example.alex.wouldyoupressthebutton.MainActivity.putText(MainActivity.java:175)
                                                       at com.example.alex.wouldyoupressthebutton.MainActivity.onCreate(MainActivity.java:85)
                                                       at android.app.Activity.performCreate(Activity.java:6757)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6316) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

Here is also my string.xml

<string-array name="text1">
    <item>You would have everything you want</item>
    <item>You can go back in time when you want</item>
    <item>You gain the ability to communicate with animals</item>
    <item>You gain any one super-power of your choice (aside from immortality)</item>
    <item>You would become the greatest player in your favorite sport</item>
    <item>You gain omniscience (infinite knowledge of everything)</item>
    <item>You become the most talented musician in the world (any instrument)</item>
    <item>Instead of Dying, you regenerate</item>
    <item>You wil have unlimited money</item>
    <item>Everyone on earth love and adore you</item>
</string-array>
<string-array name="text2">
    <item>you don\'t understand the human language anymore.</item>
    <item>you can only talk to one person per each travel.</item>
    <item>you lose the ability to communicate with humans</item>
    <item>you lose your ability to swim</item>
    <item>you would constantly be criticized</item>
    <item>you have it for 24 hours, and afterwards you gain permanent down syndrome.</item>
    <item>you cannot earn any money from your talent.</item>
    <item>you only can regenerate 12 times, and each time you look like a different person but retain all memories.</item>
    <item>you can never sleep again.</item>
    <item>never fall in love yourself.</item>
</string-array>

I have tried a shorter text in the String.xml but I still got the message... And this only happets between six and the MAX(9)

any suggestions why?

MrMinemeet
  • 304
  • 6
  • 17
  • If you're getting this while using RecyclerView.Adapter class on Android, make sure you're returning your list's size in getItemCount method, and not returning some hardcoded int value. – M.Ed Jan 08 '22 at 22:11

2 Answers2

2

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

Means you are trying to access element at index 6 of an array with length 6. But arrays are 0-based indexed, meaning that the first element is in position 0 not 1. So the maximum index in a 6-element array is index 5.

Your fault is probably in this line:

if(selecter >= MAX)

it should be

if(selecter > MAX)

However you don't show what is the MAX value. It cannot be >6. Somewhere in your code you should have defined it as int MAX = 6;

pleft
  • 7,567
  • 2
  • 21
  • 45
  • the thing is that there are 9 entries in the string.xml so I have to start at 0 and go to 9 and why does it say that it has only 6 – MrMinemeet Sep 11 '17 at 18:03
  • Yes, I observed that, have you debuged your app to see what is the true content of the arrays `text1` and `text2`? A simple `Log.d("TAG", text1.length)` will reveal you the real size of the array – pleft Sep 11 '17 at 18:06
  • When I try it on a Virtual Device it says that its 10 long and it works to **selecter = 9**... but when I try it on my LG G6 or any other real device it crashes and says that its only 6 long. – MrMinemeet Sep 12 '17 at 18:33
-1

You could increase your value of MAX. Maybe MAX = MAX + 1;