[Solved] NullPointerException on PlayerQuitEvent

When trying to get the player that quit using the PlayerQuitEvent i get a NullPointerException. Any ideas why this happens? (Besides “the player does not exist”, because i don’t think that is a valid reason just because the player class has a function called isOnline() so it should exist even though it is not online)
Here is the code:

@Subscribe
public void onUserQuit(PlayerQuitEvent event) {
    BewomUser.getUser(event.getEntity().getName()).remove();
}

Here is the error:

We need to see whatever BewomUser is.

I’m fairly certain the issue isn’t with the event, but with BewomUser#getUser()#remove()

Bewom#getUser() is being accessed statically, so it’s unlikely that the NullPointerException is being hit specifically at that line. It’s likely what’s being returned from Bewom#getUser() is null, so when you attempt to ..remove(), you’re attempting to run a method on a null value, thus the NullPointerException.

So, as @DotDash said, we need to see the Bewom class. Not to mention it’d probably help if you posted the entire stacktrace, and not just the bits that look relevant.

Ok, I see what’s going on. Apparently you can’t remove an object from a List from within that same object. So if you call objects.remove(this) it gives an error or something. This is my guess because i turned remove() into a static method and now i get no error at all.

When in doubt, separate all calls to separate lines.

@Subscribe
public void onUserQuit(PlayerQuitEvent event) {
    Player player = event.getEntity();
    String name = player.getName();
    BewomUser bUser = BewomUser.getUser(name);
    bUser.remove();
}

Or for more fun, print each one as well!

1 Like

Okay, thanks for the advice. I will make sure to do that next time :slight_smile: