i'm getting error when try run code. (to make easy see skipped of other lines didn't mentioned code line)
03-26 22:23:51.800 2425-2425/? e/rcpmanagerservice: packagereceiver onreceive() failed 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 start activity componentinfo{com.example.bilguun.pengling2/com.example.bilguun.pengling2.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2436) @ at com.example.bilguun.pengling2.mainactivity.oncreate(mainactivity.java:71)
as can see error occurs line 71 of oncreate function i'm trying instantiate classes called fault said in other questions in site. here code (the reason why declared outside need these classes in many other functions , 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 code of class fault. reason of serializable is, need pass 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 , multiple others, found no solution
nullpointerexception when creating array of objects
so questions are
- can declare array outside of class, while instantiating in oncreate function?
- is there other mistakes other array declaration might make such error. in other words can other parts of code may causing error?
- since there no main function should declare , instantiate variables these?
- since i've been learning android studio 4 days far, youtube videos, i'm still not confident java , android (because object oriented language know c++). if don't mind, can please suggest book android application development can give me systematic knowledge? , thank spending time on mess.
there 1 obvious problem here. you're accessing element index 14 in array of size 14. arrays zero-based, there no element index 14. 14 elements indexed 0 13. it's easier use length property of array make sure loop within bounds:
faults=new fault[14]; for(int i=0; < faults.length; i++){ faults[i]=new fault("unknown"); }
Comments
Post a Comment