0

I'm getting this error when I try to run my code. (To make it easy for you to see I skipped most of other lines which didn't mentioned my code line)

03-26 22:23:51.800 2425-2425/? E/RCPManagerService:  PackageReceiver onReceive() Failed to load meta-data, NullPointer: null
03-26 22:23:53.950 15700-15700/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.example.bilguun.pengling2, PID: 15700
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilguun.pengling2/com.example.bilguun.pengling2.MainActivity}: java.lang.NullPointerException
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
                                                   at at com.example.bilguun.pengling2.MainActivity.onCreate(MainActivity.java:71)

As you can see error occurs Line 71 of onCreate function where I'm trying to instantiate my classes called Fault as said in other questions in this site. Here is the Code (The reason why I declared outside is I need these classes in many other functions and activities)

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Faults=new Fault[14];
    for(int i=0;i<15;i++){
        Faults[i]=new Fault("Unknown");
    }
}
Fault[] Faults;

Here is the code of class Fault. The reason of Serializable is, I need to pass it to other activities.

 class Fault implements Serializable{
    public String fault_name="Not known";
    public int L_number=0,T_number=0;
    public String path="b";
    public Fault(String name){
        this.fault_name=name;
    }
}

I've tried every possible ways included following link and multiple others, but found no solution

NullPointerException when Creating an Array of objects

So my questions are

  1. Can I declare an array outside of the class, while instantiating it in OnCreate function?
  2. Is there any other mistakes other than array declaration that might make such error. In other words can other parts of code may causing this error?
  3. Since there is no main function where should I declare and instantiate variables like these?
  4. Since I've been learning Android Studio for 4 days so far, from Youtube videos, I'm still not confident with Java and Android (because only Object Oriented language I know is C++). If you don't mind, Can you please suggest book about Android application development which can give me systematic knowledge? And thank you for spending your time on my mess.
Community
  • 1
  • 1
Beku Ch
  • 79
  • 8

1 Answers1

1

There is one obvious problem here. You're accessing an element with index 14 in an array of size 14. Arrays are zero-based, so there is no element with index 14. The 14 elements are indexed from 0 to 13. It's easier to use the length property of the array to make sure the loop is always within bounds:

Faults=new Fault[14];
for(int i=0; i < Faults.length; i++){
    Faults[i]=new Fault("Unknown");
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441