NullPointer在某些设备上获取数组大小时出现异常

时间:2014-11-13 11:19:30

标签: java android arrays nullpointerexception

获取数组大小时,我得到nullpointer异常。我只在某些设备上获得nullpointer异常。

我有一个名为ExerciseStartActivity的类。我在这里称为randomize方法。

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;
}

 }

我有一个带有randomize类的FitnessExercise类: 在这个类中,当调用randomize()方法时,我得到空指针异常。

public class FitnessExercise {  private Exercise[] exercises; // Array
 with all the questions
     private int index = -1; // Index of the question
     private Random random = new Random();
     public FitnessExercise(Exercise[] exercises){
        this.exercises = exercises;
     }

     public void randomize()
    {
         int swapPos;
         Exercise message;
         for (int i = exercises.length - 1; i > 0; i--)
         {
             swapPos = random.nextInt(i + 1);
             message = exercises[i];
             exercises[i] = exercises[swapPos];
             exercises[swapPos] = message;
         }
     }

 }

以下是错误:

  

java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.ExerciseStartActivity}:
> java.lang.NullPointerException at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
> at android.app.ActivityThread.access$800(ActivityThread.java:135) at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> at android.os.Handler.dispatchMessage(Handler.java:102) at
> android.os.Looper.loop(Looper.java:136) at
> android.app.ActivityThread.main(ActivityThread.java:5001) at
> java.lang.reflect.Method.invokeNative(Native Method) at
> java.lang.reflect.Method.invoke(Method.java:515) at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at
> dalvik.system.NativeStart.main(Native Method) Caused by:
> java.lang.NullPointerException at
> com.playsimple.fitnessapp.FitnessExercise.randomize(FitnessExercise.java:34)
> at
> com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:74)
> at
> com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:67)
> at android.app.Activity.performCreate(Activity.java:5231) at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
> ... 11 more

3 个答案:

答案 0 :(得分:0)

这个问题很容易理解。只需阅读stacktrace:

Caused by: java.lang.NullPointerException at
    com.playsimple.fitnessapp.FitnessExercise.randomize(FitnessExercise.java:34) at 
    ....

您正在尝试访问实际为null的实例。我不知道第34行是什么,但可能的候选人是:

// You do not check if "exercices" is not null
for (int i = exercises.length - 1; i > 0; i--)

也许这一行会在null中返回initExercise()

loadExercises(SavingData.getDifficulty());

答案 1 :(得分:0)

您的问题可能在于以下三行:

Exercise[] exercises = loadExercises(SavingData.getDifficulty()); // may return null
myExercises = new FitnessExercise(exercises); // the null is passed to this instance
myExercises.randomize(); // the null is referenced here

您还没有包含loadExercises的代码,但根据您获得的例外,在某些情况下此方法可能会返回null。此null用于初始化FitnessExercise实例,并导致NullPointerException中的randomize()

我建议您不要返回null,而是在loadExercises中返回一个空数组。

答案 2 :(得分:0)

加载练习后null

Exercise[] exercises = loadExercises(SavingData.getDifficulty());
if (exercises == null) {
    // no exercises were loaded
    // maybe create default exercises here?

} else {
    myExercises = new FitnessExercise(exercises);
    myExercises.randomize();
    maxExercises = myExercises.nbrOfExercises();
    nextExercise();
}