从 ActionListener 返回值

Returning Value from ActionListener

我有一个实现 ActionListener 的方法。在 class 的底部,我有一些获取方法。但是,每当我在另一个 class 中调用 get 方法时,我都会收到 NullPointerException。

package com.FishingGame.Screen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HandleActions implements ActionListener{

private boolean goFishing = false, sell = false, checkW = false;
private int test = 12;

public HandleActions(){

}



void resetAllVars(){
    goFishing = false;
    sell = false;
    checkW = false;
}

public void actionPerformed(ActionEvent e) {
    String bSource = e.getActionCommand();

    resetAllVars();

    if(bSource == "go fishing"){

        System.out.println("1");
        goFishing = true;

    }else if(bSource == "sell"){

        System.out.println("2");
        sell = true;

    }else if(bSource == "check weather"){

        System.out.println("3");
        checkW = true;

    }
}

public boolean getGoFishing(){
    return goFishing;
}
public boolean getSell(){
    return sell;
}
public boolean getCheckW(){
    return checkW;
}
public int getTest(){
    return test;
}

}

public class Game implements Runnable {

HandleActions h;
Window w;

public Game() {

    w = new Window(600, 400, "Game");
    w.add(w.mainScreen());
    w.setVisible(true);
    System.out.println(h.getTest());
}

Thread thread = new Thread(this);


@Override
public void run() {


    }

public static void main(String args[]) {
    Game g = new Game();


    }

}

控制台显示错误来自 h.getTest() 调用。这与 HandleActions class 实现 ActionListener 这一事实有关吗?我可以 return 其他 class 的值。

变量未初始化

 HandleActions h;
 public Game() {
  h =new HandleActions(); //initialize
  ... 
}