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
- Can I declare an array outside of the class, while instantiating it in OnCreate function?
- 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?
- Since there is no main function where should I declare and instantiate variables like these?
- 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.