CaliforniaDreaming CaliforniaDreaming - 7 months ago 29
Java Question

Getting FATAL runtime error from LogCat

Unable to figure out why this error is being produced at runtime:

04-19 18:39:06.310 1741-1741/com.example.aaaaaj.studybuddy3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aaaaaj.studybuddy3, PID: 1741
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uuj.benstudybuddy3/com.example.uuj.benstudybuddy3.LecturerActivity}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.example.uuj.benstudybuddy3.LecturerActivity.onCreate(LecturerActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5001) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method) 


I have tried to follow the 'Caused by' features but still unable to figure things out. Below is my main activity - LecturerActivity.

package com.example.aaaaaj.studybuddy3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class LecturerActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Contact> list = new ArrayList<Contact>();
int[] image_id = {R.drawable.usericon};
String [] name, email, room;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecturer);
name = getResources().getStringArray(R.array.Lecturer_Name);
email = getResources().getStringArray(R.array.Email_Address);
room = getResources().getStringArray(R.array.Room);

int count = 0;
for (String Name : name)

{
Contact contact = new Contact(image_id[count],Name,email[count],room[count]);
count++;
list.add(contact);
}

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter(adapter);

}
}


I would really appreciate it if someone could give me a point in the right direction regarding this issue - incredibly frustrating! And I know its probably something very simple.

<resources>
<string name="app_name">StudyBuddy3</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_lecturer">LecturerActivity</string>

<string-array name="Lecturer_Name">

<item>"Dr. Toby Flenderson"</item>
<item>"Dr. Michael Scott"</item>
<item>"Dr. Dwight Schrute "</item>
<item>"Dr. Jim Halpert"</item>

</string-array>

<string-array name="Email_Address">

<item>"t.flenderson@dundermifflin.com"</item>
<item>"m.scott@dundermifflin.com"</item>
<item>"dk.schrute@dundermifflin.com"</item>
<item>"j.halpert@dundermifflin.com"</item>

</string-array>

<string-array name="Room">

<item>"16J14"</item>
<item>"16J12"</item>
<item>"16J15"</item>
<item>"16J16"</item>

</string-array>



Answer

The issue is that you have 3 different arrays with different number of elements but you are running loop only for name array length and this causes the ArrayIndexOutOfBoundsException as the other 2 arrays or one of the other 2 arrays have less number of elements than name array.

    name = getResources().getStringArray(R.array.Lecturer_Name);
    email = getResources().getStringArray(R.array.Email_Address);
    room = getResources().getStringArray(R.array.Room);

All the 3 declared arrays in your xml should have same number of elements in order to make your code work . Other wise you have to change your logic.

Now that you've posted your array xml, it is clear that you all the array lengths are same. The problem is with this line -

Contact contact = new Contact(image_id[count],Name,email[count],room[count]);

because as in

int[] image_id = {R.drawable.usericon};

there is only one item in image_id but you are trying to access wrong index. You can change your image_id to contain 4 elements as other arrays or change in your loop -

 Contact contact = new Contact(image_id[0],Name,email[count],room[count]);
Comments