I'm doing a project. I have a couple of classes: Course, Instructor, Student. The Course class doesn't contain any arrays in it but the Instructor class contains an array of type Course
public class Instructor {
static private int ID = 0;
static private int nofi = 0;
private String FName, title, LName;
private Course[] c1;
public Instructor(String t, String fn, String ln, Course[] c1_1, int noc)
{
title = t;
FName = fn;
LName = ln;
//ID += 1;
nofi = 0;
c1 = new Course[noc];
c1 = c1_1.clone();
}
public String getFName()
{
return FName;
}
public String getLName()
{
return LName;
}
public String getTitle()
{
return title;
}
public int getID()
{
return ID;
}
public Course[] getC1()
{
return c1;
}
public int getNofi()
{
return ++nofi;
}
}
I'm trying to use the getC1
method which is an array of type Course
.
What I did in my main activity is that I made an array object of type Instructor
and I used Intent Serializable inside a list to send the whole array of Instructor to my Second Activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
intent.putExtra("instructor class", (Serializable)i);
startActivity(intent);
}
}
});
And in my SecondActivity I receive it in this way:
Intent intent2 = getIntent();
Instructor x[];
x = (Instructor[])intent2.getSerializableExtra("instructor class");
x[0].getC1();
But when I try to run the app, it crashes right away at
x[0].getC1();
it tells me Attempt to read from a null array
.
However I removed all the nulls from my code and its fine. I don't know how to solve this or if the Syntax is right or wrong. How can I return an array?
The error message:
04-10 15:48:36.684 26089-26089/? I/art: Late-enabling -Xcheck:jni
04-10 15:48:36.816 26089-26089/com.example.saa0d.kuattend W/System: ClassLoader referenced unknown path: /data/app/com.example.saa0d.kuattend- 1/lib/arm64
04-10 15:48:36.873 26089-26089/com.example.saa0d.kuattend W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-10 15:48:36.957 26089-26089/com.example.saa0d.kuattend D/AndroidRuntime: Shutting down VM
04-10 15:48:36.958 26089-26089/com.example.saa0d.kuattend E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.saa0d.kuattend, PID: 26089
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saa0d.kuattend/com.example.saa0d.kuattend.SecondActivity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at com.example.saa0d.kuattend.SecondActivity.onCreate(SecondActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6682)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
This is the error message in the android studio: