java - NullPointer Exception when getting array size on some devices -
i nullpointer exception when getting size of array. nullpointer exception on device.
i have class called exercisestartactivity. here call randomize method.
public class exercisestartactivity extends activity{ private exercise currentexercise; private fitnessexercise myexercises; private int exercisenbr; private int maxexercises; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //.. .... .... // initexercise(); } private void initexercise() { exercisenbr = 0; exercise[] exercises = loadexercises(savingdata.getdifficulty()); myexercises = new fitnessexercise(exercises); myexercises.randomize(); maxexercises = myexercises.nbrofexercises(); nextexercise(); } private exercise[] loadexercises(int difficulty) { exercise[] exercise = null; switch(difficulty){ case r.id.radio_easy: exercise = difficultyeasy(); break; case r.id.radio_medium: exercise = difficultymedium(); break; case r.id.radio_hard: exercise = difficultyhard(); break; } return exercise; } private exercise[] difficultyhard() { exercise[] exercises = {new exercise("push-ups", "pushup", 40), new exercise("leg-up crunches", "crunch_leg_up", 90), new exercise("crunches", "crunch", 90), new exercise("bicycle crunches", "crunch_cycle", 100), new exercise("jumping jacks", "jumpingjack", 100), new exercise("squats", "squats", 30), new exercise("high knees", "highknees", 120), new exercise("lunges", "lunge", 30), new exercise("bench dips", "bench_dips", 40), new exercise("knee push-ups", "kneepushups", 80) }; return exercises; } }
i have fitnessexercise class randomize class: in class null pointer exception when randomize() method called.
public class fitnessexercise { private exercise[] exercises; // array questions private int index = -1; // index of question private random random = new random(); public fitnessexercise(exercise[] exercises){ this.exercises = exercises; } public void randomize() { int swappos; exercise message; (int = exercises.length - 1; > 0; i--) { swappos = random.nextint(i + 1); message = exercises[i]; exercises[i] = exercises[swappos]; exercises[swappos] = message; } } }
here errors:
java.lang.runtimeexception: unable start activity > componentinfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.exercisestartactivity}: > java.lang.nullpointerexception @ > android.app.activitythread.performlaunchactivity(activitythread.java:2184) > @ > android.app.activitythread.handlelaunchactivity(activitythread.java:2233) > @ android.app.activitythread.access$800(activitythread.java:135) @ > android.app.activitythread$h.handlemessage(activitythread.java:1196) > @ android.os.handler.dispatchmessage(handler.java:102) @ > android.os.looper.loop(looper.java:136) @ > android.app.activitythread.main(activitythread.java:5001) @ > java.lang.reflect.method.invokenative(native method) @ > java.lang.reflect.method.invoke(method.java:515) @ > com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) > @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ > dalvik.system.nativestart.main(native method) caused by: > java.lang.nullpointerexception @ > com.playsimple.fitnessapp.fitnessexercise.randomize(fitnessexercise.java:34) > @ > com.playsimple.fitnessapp.exercisestartactivity.initexercise(exercisestartactivity.java:74) > @ > com.playsimple.fitnessapp.exercisestartactivity.oncreate(exercisestartactivity.java:67) > @ android.app.activity.performcreate(activity.java:5231) @ > android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) > @ > android.app.activitythread.performlaunchactivity(activitythread.java:2148) > ... 11 more
after loading exercises null
.
exercise[] exercises = loadexercises(savingdata.getdifficulty()); if (exercises == null) { // no exercises loaded // maybe create default exercises here? } else { myexercises = new fitnessexercise(exercises); myexercises.randomize(); maxexercises = myexercises.nbrofexercises(); nextexercise(); }
Comments
Post a Comment