-1

I'm answering a practice problem in java and I've created some code. However, when I run it I get an error and I'm not sure what the error means

I've tried re-reading the code, but I still don't know what the error is:

Exception in thread "main" java.lang.NullPointerException
  at SugarSmashPlayer.setHighestScore(SugarSmashPlayer.java:42)
  at Main.main(Main.java:7)
Process finished with exit code 1
public class Main {

    public static void main(String[] args) {
        SugarSmashPlayer Jacob = new SugarSmashPlayer();
        Jacob.setHighestScore(40,1);
        System.out.println(Jacob.getHighestScore(1));
    }

}


public class SugarSmashPlayer {
    private int IDnumber;
    public String screenName;
    public int[] highestScore;
    public int level;


    public int getIDnumber() {
        return IDnumber;
    }

    public void setIDnumber(int IDnumber) {
        this.IDnumber = IDnumber;
    }

    public String getScreenName() {
        return screenName;
    }

    public void setScreenName(String screenName) {
        this.screenName = screenName;
    }

    public int getHighestScore(int level) {
        if(level > 10 || level < 1) {
            System.out.println("Invalid level");
            return 0;
        } else {
            return highestScore[level-1];

        }
    }

    public void setHighestScore(int highestScore, int level) {
        if(level > 10 || level < 1) {
            System.out.println("Invalid level");
        } else {
            if(level > 2 && this.highestScore[level-2] > 99) {
                this.highestScore[level - 1] = highestScore;
            } else {
                if (level == 1) {
                    this.highestScore[level - 1] = highestScore;
                } else {
                    System.out.println("You have not achieved a score of 100 or greater in the previous level");
                }
            }
        }
    }
}

I expected the highscore for level 1 to be set to 40 and when I gethighscore I expected it to display 40

tkruse
  • 10,222
  • 7
  • 53
  • 80
Abc
  • 43
  • 1
  • 8
  • 2
    Because you have an array reference with no space allocated for it. – Dave Newton Aug 06 '19 at 22:33
  • might be more convenient to use an ArrayList of integers here... ArrayList highScores = new ArrayList<>(); You can then add/remove/sort, etc... – pcalkins Aug 06 '19 at 22:43

1 Answers1

4

An int[] is an object type, so when you declare your field

public int[] highestScore;

what you get is a field that can contain a reference to an int[] object. However, you never actually put such a reference into the field, so its value stays at its default, which is null.

Therefore you get a NullPointerException when you start using the field as if it contained a reference to an actual array object you can index into.

Amend your code such that at an appropriate place you allocate an actual array and put the reference to it into highestScores:

highestScores = new int[117];
hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73