Hong Hong - 1 year ago 90
Java Question

Returning Value from ActionListener

I have a method that implements ActionListener. At the bottom of the class I have some get methods. However whenever I call the get methods in another class, I get a 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();


if(bSource == "go fishing"){

goFishing = true;

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

sell = true;

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

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");

Thread thread = new Thread(this);

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. Is it something to do with the fact that the HandleActions class implements ActionListener. I can return values just fine from other classes.

Answer Source

the variable is uninitialized

 HandleActions h;
 public Game() {
  h =new HandleActions(); //initialize
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download