-1

I am making a game that requires an update class to access a game class and a main class to access both of those. The problem I am having is that I need the update class to have an updated object of the game class but I get an error whenever I try and access the game class from the Update class [error occurs on newGame.test();]

ERROR: Exception in thread "main" java.lang.NullPointerException
    at Updates.updateStats(Updates.java:17)
    at Game.gameLoop(Game.java:24)
    at Main.main(Main.java:14)


import java.util.Scanner;

public class Main 
{ 
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args)
    {
        Game newGame = new Game();
        //Updates getUpdates = new Updates();

        newGame.setupGame();
        Game.isRunning=true;
        newGame.gameLoop();

    }

}


import java.util.Scanner;

public class Game {

    static Scanner input = new Scanner(System.in);

    Updates getUpdates = new Updates();

    public Game(){

    }


    String goverment;
    int happyness;
    double money;
    int population = 1000000;

    public static boolean isRunning;
    private int turn = 0;

    public void gameLoop(){
        while (isRunning){
            getUpdates.updateStats();
            System.out.println("Turn: "+turn);
            input.nextLine();
            turn++;
        }
    }

    public void setupGame()
    {
        System.out.println("Goverment: 1=Democracy 2=monarchy 3=dictatorship");
        goverment = input.nextLine();
        while (!goverment.equals("1")||!goverment.equals("2")||!goverment.equals("3")){
            if (goverment.equals("1")){
                happyness = 75;
                money = 250000.0;
                break;
            }
            else if (goverment.equals("2")){
                happyness = 50;
                money = 500000.0;
                break;
            }
            else if (goverment.equals("3")){
                happyness = 25;
                money = 750000.0;
                break;
            }
            else{
                System.out.println("ENTER A VALID VALUE");
                goverment = input.nextLine();
            }

        }

        System.out.println("1");

    }

    public int getHappyness(){
        return happyness;
    }

    public void test(){
        System.out.println("MY NAME IS BOB");
    }
}





import java.util.Scanner;

public class Updates {

    static Scanner input = new Scanner(System.in);

    public Updates(){
    }

    public Updates(Game newGame){
        this.newGame = newGame;
    }

    Game newGame;

    public void updateStats(){
        newGame.test();

    }
}
James Clarke
  • 29
  • 1
  • 5

1 Answers1

0

I'm sorry if this isn't very much help, but it's my first time answering a question on here.

I put your code into a test project to see where the issue was, and it seems you have a few errors.

I'll start with the Main class as it has the smallest issues.

You don't need to declare the scanner object here as it's never used. You're just allocating memory to an empty object.

Now, onto the Updates class.

Again, no need to declare a scanner here.

To use the object "newGame" you need to make sure you're making use of the Constructor:

public Updates(Game newGame){
    this.newGame = newGame;
}

And not:

public Updates(){

}

Because the latter one won't set the Game object for you, and any time you access it you will get a nullpointer.

Finally, the Game class:

I'd make both the Scanner and the Updates objects private, as they're never used outside the class. If they are, make use of getters and setters.

In your Game constructor, you can actually create both the getUpdates and input objects, like so:

public Game() {
    this.input = new Scanner(System.in);
    this.getUpdates = new Updates(this);
}

That way whenever your game is initialized, you'll have them at the ready. As you can see, I put

new Updates(this)

which uses the Updates constructor I mentioned before. This should ultimately solve your issue.

For reference, here are the files I used/edited and it worked on my end:

Pastebin link

Community
  • 1
  • 1
Jesse
  • 13
  • 5
  • thank you so much! Because i'm new to java i never would have realised about these issues, ill try the new code and compile it and see what happens. – James Clarke Nov 13 '16 at 14:00