简体   繁体   English

从ActionListener返回值

[英]Returning Value from ActionListener

I have a method that implements ActionListener. 我有一个实现ActionListener的方法。 At the bottom of the class I have some get methods. 在类的底部,我有一些get方法。 However whenever I call the get methods in another class, I get a NullPointerException. 但是,每当我在另一个类中调用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();


    }

}

The console says the error is coming from the h.getTest() call. 控制台说该错误来自h.getTest()调用。 Is it something to do with the fact that the HandleActions class implements ActionListener. 与HandleActions类实现ActionListener的事实有关吗? I can return values just fine from other classes. 我可以从其他类中返回正确的值。

the variable is uninitialized 变量未初始化

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM