Java - refer to the implemented class

I have a small problem: I developed an "ExampleQuest" class for my game that extends the abstract Quest class. I did this to create separate quest classes. Now my ExampleQuest class should count the death of my entities, so I implemented my EntityListener. Now I have to register it in my Playstate class for everything to work, but here is my actual problem: the Playstate.addEntityListener (this) method is giving me a nullpointer exception. I found out that this error is caused by any extended class . If ExampleQuest doesn't distribute Quest, everything will work correctly. There is nothing wrong with my Quest class, because if I extended ExampleQuest with something else, I would also get a nullpointer exception.

---> So my explanation is that this one from Playstate.addEntityListener (this) refers to the extended class in this case Quest and not the EntityListener. How can I solve my problem?

public class ExampleQuest extends Quest implements EntityListener {

    public ExampleQuest() {
        super();
        Playstate.addEntityListener(this); //gives me nullointer exception 
    }

    //implemented method
    public void entityDeathEvent(EntityEvent e) {

    }
}

      

This is part of my Playstate class:

public class Playstate {

    public static Set<EntityListener> entityListener;

    public Playstate() {
        entityListener = new HashSet<EntityListener>();
    }

    public static void addEntityListener(EntityListener listener) {
        entityListener.add(listener);
    }
}

      

EDIT: this works correctly:

public class EventHandler implements EntityListener {

    public EventHandler() {
        Playstate.addEntityListener(this);
    }
}

      

works because EventHandler only implements the class

+3


source to share


3 answers


The reason you are getting NPE is because it has entityListener

n't been initialized. The reason it has entityListener

n't been initialized is because your code requires instantiation Playstate

before the method can be used addEntityListener

, but you call addEntityListener

before instantiating Playstate

.

This is incorrect: static variables should not be initialized in instance constructors. You need to do it either in declarations like this

public static Set<EntityListener> entityListener = new HashSet<EntityListener>();

      



or into static

initializer blocks, for example:

public static Set<EntityListener> entityListener;

static {
    entityListener = new HashSet<EntityListener>();
}

      

Creation entityListener

will work with an instance variable as well, but you will need to provide a way to get an instance Playstate

from the constructor context ExampleQuest

.

+1


source


Your field entityListener

is null because it is static and you only initialize the field when you create the object Playstate

.

Probably neither entityListener

nor addEntityListener

should it be static. Make them members of the instance.

public class Playstate {

    public Set<EntityListener> entityListener;

    public Playstate() {
        entityListener = new HashSet<EntityListener>();
    }

    public void addEntityListener(EntityListener listener) {
        entityListener.add(listener);
    }
}

      



Regarding your editing: We can only assume that when you execute your code, EventHandler

it works because you've already created an object Playstate

somewhere else in your code.

Using a static event listener for Playstate

means that all such objects will share event listeners, which is a → terrible <idea. Indeed, make them instance members, you will be much better off.

+1


source


public Playstate() {
    entityListener = new HashSet<EntityListener>();
}

public static void addEntityListener(EntityListener listener) {
    entityListener.add(listener);
}

      

The entityListener static variable is assigned a value when the PlayState constructor is constructed. This is a serious design issue: either the listener is static and therefore not associated with any PlayState instance and therefore should not be assigned every time an instance is created or associated with a given instance, and it should not be static.

The listener and method addEntityListener()

must not be static. Instead, quests must have a reference to an instance of the PlayState class.

Also, since this is a set of listeners, the variable must have a name entityListeners

, not entityListener

.

0


source







All Articles