Error: Could not pass the event MoveEntityEvent$Position$Impl to an Event Listener!

Hi everyone :smile:, I need help, yesterday I programmed a piece of code that allows me to know if a player is in a certain area with his coordinates, however in my code I had an error that I never get and I can’t fix it.
(I looked for the mistake on the internet, but I could hardly find anything).

Here is the error message:

Could not pass the event MoveEntityEvent$Position$Impl to an Event Listener! Since the event can be modified during the event's listener, there could be adverse side effects of the exception occuring such that duplications or other game breaking issues could exist. Due to the nature of the exception, this is not likely an exception that is covered by SpongeForge or Forge itself, and therefor should be reported to the mod/plugin author first prior to reporting to Sponge or Forge. */

Owning Mod/Plugin: pointmanager
Exception:
    java.lang.NullPointerException: null
         fr.pitiprince.pointsmanager.MainPointManager.onPlayerMove(MainPointManager.java:82)

This is my simplified program:

private Map<String,String> allPlayerStates;

@Listener
public void onPlayerMove(MoveEntityEvent event) {
	if (event.getTargetEntity() instanceof Player) { // if entity is player
		Player player = (Player) event.getTargetEntity(); // get player
		
		allPlayerStates.containsKey(player.getUniqueId().toString()); // check if player is in Map<>
	}
}

Thank you !

Are you sure allPlayerStates is not null?

It should be initialized first, for example: private Map<String,String> allPlayerStates = new HashMap<>();

Yes I’m sure allPlayerStates is not Null, but I get the same error as soon as I try to send player to a method, but not all of them, I don’t understand why… for example I created a class that manages the SQL of the plugin, but when I send player to one of its method to get a registered value I get the same error.

It would be a lot easier if we saw the real code. If you don’t want to share the whole code. Then share the lines starting from the method init to line 82 of MainPointManager specifying what line is line 82 is.

If you dont want to share the actual code in any form that all we can do is offer suggestions on finding the issue.

So NullPointerException is where a variable or method return has no value, meaning that a object has not been created. Therefore as errors go, its typically one that is simple to fix.

You can find the value of each variable (including if it is null) by simply putting the variable in a String as is. For example

Integer a = 0;
a = null;
System.out.println(a + "");

If the variable has a value of null then the output would be null, however if it wasnt null then it would use the objects .toString() function.

With that you can find out what value is returning null in your “simple code example” by the following

private Map<String,String> allPlayerStates;

@Listener
public void onPlayerMove(MoveEntityEvent event) {
if (event.getTargetEntity() instanceof Player) { // if entity is player
	Player player = (Player)  event.getTargetEntity(); // get player
	
	System.out.println("AllPlayerState: " + allPlayerState);
    System.out.println("Player: " + player);  
    System.out.println("Unique ID: " + player.getUniquieId());

   allPlayerStates.containsKey(player.getUniqueId().toString()); // check if player is in Map<>
    }
}

This is a form of debugging, however if you prefer there is real debugging as a option where you can use the forge client with Sponge and there you get a live feed of all the variables, fields, etc and what the value is of them. You can put break points in your code too so when that part of your code gets hit then the game pauses for you to see whats going on.

It is also possible to get real debug on the server too if your IDE supports it. Intelij does and you do it by running your plugin from the IDE as a Java Application with the application to run being the server. However note that if the server pauses due to a breakpoint being hit the players will all timeout