0

I am attempting to program an onJoin() event so any user who connects gets their gamemode set to the server default. However, whenever a player joins, it raises a NullPointerException.

public class ListenerPlayerLogin implements Listener {
    @EventHandler(priority = EventPriority.HIGH) // This needs to run correctly.
    public void onLogin(PlayerLoginEvent event) {
        Logger console = Bukkit.getLogger();
        console.info("onLogin called.");
        Server server = Bukkit.getServer();
        GameMode defaultGamemode = server.getDefaultGameMode();
        console.info("Default Gamemode: " + defaultGamemode.toString());
        HumanEntity player = event.getPlayer();
        console.info("Target: " + player.getName());
        player.setGameMode(defaultGamemode);
        String playGM = player.getGameMode().toString();
        console.info("Player Gamemode: " + playGM);
        player.sendMessage("§eRandomos framework loaded.");
    }
}

The error I'm getting is:

org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.PlayerList.attemptLogin(PlayerList.java:513) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.LoginListener.c(LoginListener.java:134) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.LoginListener.tick(LoginListener.java:53) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.NetworkManager.a(NetworkManager.java:220) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.ServerConnection.c(ServerConnection.java:129) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.b(MinecraftServer.java:1105) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:399) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:984) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:824) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
Caused by: java.lang.NullPointerException
        at uk.kaidev.randomos.ListenerPlayerLogin.onLogin(ListenerPlayerLogin.java:25) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
        ... 13 more```
  • 3
    `PlayerLoginEvent` is called while `Player` is still being initialized, use `PlayerJoinEvent` – Rogue Jun 05 '20 at 01:12
  • @Rogue Post an answer, instead of a comment. Otherwise, this will never be marked as solved. – TCoded Jun 13 '20 at 21:55
  • @TCoded this question is closed and not accepting answers. The basic premise of the question is the same as the linked duplicate (`Player` is null, and someone is invoking methods on it). The only oddity is that player is null _because_ of using the wrong event. If the question is edited to focus on that aspect (and thus able to reopen), I could post it as an answer at that time – Rogue Jun 13 '20 at 23:22

0 Answers0