I've been having trouble with reading data in from a text file to a 2D array. I am not too sure what the issue with the out of bounds exception is because I thought I initialized it correctly but I am not sure. Here is my code.
String [][] f_team = new String [20][6]; //20 and 6
int cnt = 0;
int cnt2 = 0;
String line = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadData();
}
public void loadData()
{
try {
BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("team.txt")));
while(cnt<=20) {
line = br.readLine();
f_team[cnt][cnt2] = line;
cnt2 = cnt2 + 1;
if (cnt2 == 5) {
cnt = cnt + 1;
cnt2 = 0;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my logcat for the error: 05-16 17:33:29.119 26312-26312/com.jmac.jmac.footy E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jmac.jmac.footy, PID: 26312 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jmac.jmac.footy/com.jmac.jmac.footy.Pitch}: java.lang.ArrayIndexOutOfBoundsException: length=20; index=20 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.inteal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.inteal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.ArrayIndexOutOfBoundsException: length=20; index=20 at com.jmac.jmac.footy.Pitch.loadData(Pitch.java:82) at com.jmac.jmac.footy.Pitch.onCreate(Pitch.java:74) at android.app.Activity.performCreate(Activity.java:6289) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.inteal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.inteal.os.ZygoteInit.main(ZygoteInit.java:1194)
