0

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:

This is the error message in the android studio

mortalis
  • 2,060
  • 24
  • 34
SwiftyDude
  • 19
  • 3
  • add your stacktrace – Zahan Safallwa Apr 10 '17 at 11:48
  • what do you mean by stack trace? – SwiftyDude Apr 10 '17 at 11:52
  • the error you are getting when the app crashes – Zahan Safallwa Apr 10 '17 at 11:53
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 10 '17 at 13:15

3 Answers3

0

implement serializable

 public class Instructor implements Serializable
  {
  }

you pass Instructor class object to next activity not array of Instructor class

    x = (Instructor[])intent2.getSerializableExtra("instructor class");

change to

    x = (Instructor)intent2.getSerializableExtra("instructor class");

one more thing use key without any space like this

"instructor_class"
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
0

You should make your classes implement Parcelable and after that use intent.putParcelableArray("instructor class", i); Parcelable is faster than Serializable

Another way is to keep using Serializable and try to replace Instructor[] to ArrayList, but I'm not sure this will help

Ivan Rudnev
  • 380
  • 7
  • 12
  • what is the difference? My teacher has told me to use Serializable because it can transfer everything in one fell swoop. – SwiftyDude Apr 10 '17 at 12:11
  • Serializable uses Reflection and it's much slower. Nevertheless have you tried to make Course class implement Serializable? – Ivan Rudnev Apr 10 '17 at 12:15
  • Try to use Bundle with ArrayList as shown here: http://stackoverflow.com/a/14333555/7027883 – Ivan Rudnev Apr 10 '17 at 12:19
0

@Hitesh Gehlot thank you man

       x = (Instructor)intent2.getSerializableExtra("instructor class");

This was part of the problem, it shouldn't be an array. The main problem was that when I come to run the app, my debugged was set to run the SecondActivity only without the main one. So nothing was passed thats why it was null.

SwiftyDude
  • 19
  • 3