-1

I wanted to lead this question by letting everyone know that this is my first time on stack overflow so if I do not conform to question-asking standards please let me know.

I'm making a program that plays Rock, Paper, Scissors with you, and right as I was approaching the back end of the project this error came up:

Exception in thread "main" java.lang.NullPointerException
  at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
  at RockPaperScissors.main(RockPaperScissors.java:26)

I'm not sure where I would be using null, but thats what you're here for.

Here is the entire project compiled as it currently is:

public class RockPaperScissors {
//sets the constants
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;

//creates some variables
static int playerThrow, computerThrow, result, timesPlayed, playerWins, computerWins;
static String playAgain;
static Scanner fru;

/*
 * The Results
 * 0 = tie
 * 1 = Player win
 * 2 = Computer win
*/

public static void main(String[] args) {
    //this do while loop is the whole game
    do {
        //decides the throws of the players
        playerThrow = getPlayerThrow();
        computerThrow = (int)(Math.random() * 3 + 1);

        switch(playerThrow) {
        //compares and displays the computer and player
        //choices if the player chooses rock
        case ROCK:
            switch(computerThrow) {
            case ROCK:
                result = 0;
                System.out.println("You threw rock and the computer threw rock!");
                break;
            case PAPER:
                result = 2;
                System.out.println("You threw rock and the computer threw paper!");
                break;
            case SCISSORS:
                result = 1;
                System.out.println("You threw rock and the computer threw scissors!");
                break;
            }   break;
        //compares and displays the computer and player
        //choices if the player throws paper
        case PAPER:
            switch(computerThrow) {
            case ROCK:
                result = 1;
                System.out.println("You threw paper and the computer threw rock!");
                break;
            case PAPER:
                result = 2;
                System.out.println("You threw paper and the computer threw paper!");
                break;
            case SCISSORS:
                result = 3;
                System.out.println("You threw paper and the computer threw scissors!");
                break;
            }   break;
        //compares and displays the computer and player
        //choices if the player throws scissors
        case SCISSORS:
            switch(computerThrow) {
            case ROCK:
                result = 2;
                System.out.println("You threw scissors and the computer threw rock!");
                break;
            case PAPER:
                result = 1;
                System.out.println("You threw scissors and the computer threw paper!");
                break;
            case SCISSORS:
                result = 0;
                System.out.print("You threw scissors and the computer threw scissors!");
                break;
            }   break;
        }
        timesPlayed ++;

        // will compare and decide the winner of the two players
        finish();

    } while (timesPlayed < 3);
}

public static int getPlayerThrow() {
    //prompts weapon choice and stores said choice
    System.out.println("Choose your weapon of choice:\n(1 for rock, 2 for paper, 3 for scissors)");
    int choice = fru.nextInt();

    //checks for validity and returns the choice
    if (choice != 1 && choice != 2 && choice != 3) {
        System.out.print("Not a valid input!\n Please try again: ");
        choice = fru.nextInt();
    }

    return choice;
}

//compares and decides the winner of the two players
public static void finish() {
    //displays the winner of the round accourding to aforementioned possible results
    switch(result) {
    case 0:
        System.out.println("Its a tie!"); break;
    case 1:
        System.out.println("You are victorious! Man over machine!");
         playerWins++; break;
    case 2:
        System.out.println("The computer has taken the round! Technological singularity approaches!");
        computerWins++; break;
    }

    //cheks if the match is over and displays messages accordingly
    switch(timesPlayed) {
    case 1: break;
    case 2:
        if (playerWins == 2 || computerWins == 2) {
            if (playerWins == 2) {
                System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
                timesPlayed = 5;
                playAgain = fru.nextLine();

                //checks for validity
                if (playAgain != "y" || playAgain != "n") {
                    System.out.print("Not a valid input!\n Please try again: ");
                    playAgain = fru.nextLine();
                }
            }
            else if (computerWins == 2) {
                System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
                timesPlayed = 5;
                playAgain = fru.nextLine();

                //checks for validity
                if (playAgain != "y" || playAgain != "n") {
                    System.out.print("Not a valid input!\n Please try again: ");
                    playAgain = fru.nextLine();
                }
            }
        } break;
    //will happen for any amount of times played over 2
    default:
        if (playerWins == 2) {
            System.out.println("You win the match! Congratulations!\nWould you like to play another match?\n(y for yes, n for no)");
            playAgain = fru.nextLine();

            //checks for validity
            if (playAgain != "y" || playAgain != "n") {
                System.out.print("Not a valid input!\n Please try again: ");
                playAgain = fru.nextLine();
            }
        }
        else if (computerWins == 2) {
            System.out.println("The computer wins the match!\nPlay again! I know you can beat it.\n(y for yes, n for no)");
            playAgain = fru.nextLine();

            //checks for validity
            if (playAgain != "y" || playAgain != "n") {
                System.out.print("Not a valid input!\n Please try again: ");
                playAgain = fru.nextLine();
            }
        }
    }
}
}

I understand neither what the error means nor where it comes from. The only information that I have about it comes from skimming google for the error, but it is tough when the questions asked or examples posted are not specific to my project. I have taken multiple steps towards fixing it but none of them seem to do anything.

This could be a complex coding problem or a single character that I have missed, but any and all help is appreciated! Thank you!

Fruot
  • 11
  • 1

1 Answers1

0

I see plenty of places where you use the fru scanner but absolutely none where you actually initialise it. What you have basically boils down to:

import java.util.Scanner;
public class Test {
    static Scanner fru;
    public static void main(String[] arg) {
        int x = fru.nextInt();
        System.out.println(x+1);
    }
}

and, when you run that, you'll see the exception. You'll need to do something like:

fru = new Scanner(System.in);

before you attempt to do any scanning. Until you do that, trying to dereference it will cause the exception you're getting. In other words, something like:

import java.util.Scanner;
public class Test {
    static Scanner fru;
    public static void main(String[] arg) {
        fru = new Scanner(System.in);
        int x = fru.nextInt();
        System.out.println(x+1);
    }
}

which runs successfully.


In terms of finding these problems in future, you would do well to look at the error message you're actually getting:

Exception in thread "main" java.lang.NullPointerException
  at RockPaperScissors.getPlayerThrow(RockPaperScissors.java:93)
  at RockPaperScissors.main(RockPaperScissors.java:26)

Despite the fact your error message doesn't quite match your code (line numbers are off by three), examining the stack trace to track down where and what the errors are is a vital skill.

In this case, it's the line:

int choice = fru.nextInt();

and you would therefore assume it's because fru has not been set correctly. From there, it a matter of tracking back to where you actually set fru to something useful. In this particular case, that doesn't exist so it's relatively easy to figure out.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • To the downvoter, your definition of "not useful" appears to be vastly different to mine. This is the *exact* problem the asker has with their code. – paxdiablo Oct 02 '15 at 03:10