【问题标题】:Java accessing another class not workingJava访问另一个类不起作用
【发布时间】:2016-11-13 13:31:36
【问题描述】:

我正在制作一个需要更新类来访问游戏类和主类来访问这两者的游戏。我遇到的问题是我需要更新类来更新游戏类的对象,但是每当我尝试从更新类访问游戏类时都会收到错误消息 [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();

    }
}

【问题讨论】:

  • 你得到什么错误/??
  • 顺便说一句,两个类都不需要使用 2 个扫描仪
  • 我猜是 NPE,因为没有 Game 永远不会传递给 Updates
  • @DaveNewton 我该如何解决这个问题?
  • 在构造函数中将Game 传递给Updates

标签: java class variables methods


【解决方案1】:

很抱歉,如果这不是很有帮助,但这是我第一次在这里回答问题。

我把你的代码放到一个测试项目中看看问题出在哪里,看来你有一些错误。

我将从 Main 类开始,因为它有最小的问题。

您无需在此处声明扫描仪对象,因为它从未使用过。您只是将内存分配给一个空对象。

现在,进入更新类。

同样,这里无需声明扫描仪。

要使用对象“newGame”,您需要确保使用的是构造函数:

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

而不是:

public Updates(){

}

因为后者不会为你设置 Game 对象,并且任何时候你访问它都会得到一个空指针。

最后是游戏类:

我会将 Scanner 和 Updates 对象都设为私有,因为它们从未在课堂外使用过。如果是,请使用getters and setters

在您的 Game 构造函数中,您实际上可以同时创建 getUpdates 和输入对象,如下所示:

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

这样,每当您的游戏初始化时,您就可以准备好它们。如你所见,我把

new Updates(this)

它使用了我之前提到的 Updates 构造函数。这应该最终解决您的问题。

作为参考,这里是我使用/编辑的文件,它对我有用:

Pastebin link

【讨论】:

  • 非常感谢!因为我是 Java 新手,所以我永远不会意识到这些问题,我会尝试新代码并编译它,看看会发生什么。
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2014-04-06
  • 2015-01-26
相关资源
最近更新 更多