Solved [HELP] For Loop

Discussion in 'Spigot Plugin Development' started by DragonLord__, Jan 28, 2020.

Thread Status:
Not open for further replies.
  1. Error:
    Code (Text):

    Could not pass event PlayerJoinEvent to VenomLR v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.PlayerList.a(PlayerList.java:173) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.LoginListener.c(LoginListener.java:154) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.LoginListener.tick(LoginListener.java:53) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.NetworkManager.a(NetworkManager.java:220) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.ServerConnection.c(ServerConnection.java:129) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1091) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:393) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:970) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:815) [Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            at java.lang.Thread.run(Thread.java:819) [?:1.8.0_232]
    Caused by: java.lang.NullPointerException
            at me.venom.lr.events.Events.onJoin(Events.java:22) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_232]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_232]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[Spigot.jar:git-Spigot-cbd1a1b-009d8af]
            ... 13 more
     
    Code:
    Code (Text):

    @EventHandler
        public void onJoin(PlayerJoinEvent e)
        {
            Player p = e.getPlayer();
            boolean pronadjen = false;
            if(plugin.enabledSQL == false)
            {
                for(String players : plugin.players.getConfigurationSection("Players").getKeys(false)) //Line with an error
                {
                    if(players.equalsIgnoreCase(p.getUniqueId().toString()))
                    {
                        plugin.registered = true;
                        pronadjen = true;
                        Bukkit.getServer().getConsoleSender().sendMessage("Igrac je pronadjen!");
                    }
                }
                if(pronadjen == false)
                {
                    Bukkit.getServer().getConsoleSender().sendMessage("Igrac nije pronadjen!");
                    plugin.registered = false;
                }
            }
            else
            {
             
            }
        }
     
    players.yml
    Code (Text):

    Players:
      //Here should be UUID when added
        //Here should be PlayerName when added, but not for now
     
    Tried: Adding UUID and everything needed so players is not empty, not fixed.

    I don't understand why and how...
     
  2. Whatever line 22 references in your Events class is null. I'd assume it's plugin.players, you probably forgot to initialize it. This is one of the reasons why you should not make variables static for easy access, but rather access them through getter and setter methods.

    Very minor side note, you don't need to check for if (boolean == false) on booleans, if(!boolean) is enough.
     
  3. Something on line 22 is null.
    As a guess it is plugin.players.getConfigurationSection("Players").getKeys(false).
    could be the "plugin" variable is null, the "players" variable found in your plugin is null, or the configuration section "Players" can not be found in your config.yml or is blank and results in being null.
     
  4. Main plugin = Main.getPlugin(Main.class) (plugin is clearly here)
    players refers to players.yml
    players = New YamlConfiguration(); in main class
    "Players" is not in config but in players.yml thats why I used plugin.players
    (Gadse, thansk for getter and setter)
     
  5. If you don't understand the exact cause, break your code down and debug it. Make a few checks for null.

    Code (Java):
    if (plugin == null) {
      System.out.println("Plugin is null");
      return;
    }

    if (plugin.players == null) {
     // etc.
    }
    You'll surely find the root cause through that :)
     
  6. Thanks, it ended up that ConfigurationSection was null. Apparently it seems like Main class didn't execute players.load(playersFile); ....
     
Thread Status:
Not open for further replies.