So this is the first time I'm writing a proper event manager but for some reason my method which calls and invokes the stored keys/values throws a NullPointerException even though I added the class and method data to the map in my register method.
My calling method:
public static final void fire(final Event event) {
final Class<? extends Event> clazz = event.getClass();
final List<EventListener> listeners = eventMap.get(clazz);
if(listeners != null) {
for(EventListener listener : listeners) {
try {
listener.getMethod().invoke(listener.getObject(), event);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
And this is where I register/unregister my events:
public static final void register(Object o) {
Class clazz = o.getClass();
Method[] methods = clazz.getDeclaredMethods();
for(Method m : methods) {
final Handler handler = m.getAnnotation(Handler.class);
if(handler == null) continue;
if(!m.isAccessible())
m.setAccessible(true);
if(!eventMap.containsKey(clazz) && m.getParameterTypes()[0].equals(clazz)) {
eventMap.put(clazz, new LinkedList<EventListener>());
}
}
}
public static final void unregister(Object o) {
Class clazz = o.getClass();
Method[] methods = clazz.getDeclaredMethods();
for(Method m : methods) {
if(eventMap.containsKey(clazz)) {
final List<EventListener> listener = eventMap.get(clazz);
eventMap.remove(clazz, listener);
}
}
Any help would be greatly appreciated.
Update: Here's the stacktrace when I remove the != null check.
java.lang.NullPointerException: Unexpected error
at me.jamie.lib.event.EventManager.fire(EventManager.java:49)
at me.jamie.revolution.gui.RevolutionUI.func_175180_a(RevolutionUI.java:25)
Like I said, the register/unregister methods run without a problem, but when I initiate the fire method it seems to consider the map as empty.
Aucun commentaire:
Enregistrer un commentaire