java.lang.NullPointerException trying to execute a Listener

Discussion in 'Plugin Development' started by TheDiamondCraver16, Oct 1, 2016.

Thread Status:
Not open for further replies.
  1. Hi, I recently have attempted to develop a plugin that strikes the player with lightning when they join if they are on a list saved in a config file. I'm getting a NullPointer error when trying to start my server. I don't exactly know whats causing the error but I know its on line 16 in LightningClass and 18 in the listener.


    Main Class -
    Code:
    package me.[REDACTED];
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
        public class LightningClass extends JavaPlugin {
             
        @Override
        public void onEnable() {
            new JoinListner(this);
         
            getConfig().addDefault("users", "");
            this.getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
        @Override
        public void onDisable() {
            saveConfig();
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { //starts a boolean that is checking for a command
         
            FileConfiguration config = this.getConfig();
         
            if (cmd.getName().equalsIgnoreCase("loj") && sender instanceof Player) { //when the command is received
             
                Player player = (Player) sender; //defines who the "player" is
             
                int length = args.length;
             
                if (length == 2) { //if command has correct amount of args
                 
                    String message = ChatColor.BLACK + "[" + ChatColor.GOLD + "LightningOnJoin" + ChatColor.BLACK + "] ";
                    if (player.hasPermission("loj.add")){
                    if (args[0].equalsIgnoreCase("add")) {
                        boolean playerFound = false; //sets the boolean to false because it has not found a player yet
                     
                        for (Player playerToAdd : Bukkit.getServer().getOnlinePlayers()) { //creates an array for the online players of the server
                            if (playerToAdd.getName().equalsIgnoreCase(args[1])) { //if a player was found matching args 1
                             
                                String uuid = playerToAdd.getUniqueId().toString();
                             
                                if (!config.contains("users." + uuid)) {
                                    config.set("users." + uuid, "");
                                    saveConfig();
                                 
                                    playerToAdd.sendMessage(message + ChatColor.YELLOW + "You were added to the Lightning on join list!");
                                    player.sendMessage(message + ChatColor.YELLOW + playerToAdd.getDisplayName() + ChatColor.YELLOW + " was added to the list successfully!");
                                    playerFound = true; //sets the boolean to true because it found a player
                                    break;
                                 
                                }
                             
                                if (config.contains("users." + uuid)) {
                                    player.sendMessage(message + ChatColor.YELLOW + "Player " + playerToAdd.getDisplayName() + ChatColor.YELLOW + " is already on the list!");
                                    playerFound = true;
                                    break;
                                }
                             
                            }
                         
                            }
                     
                        if (playerFound == false) {  //if the boolean was never set to true
                            player.sendMessage(ChatColor.RED + "Player " + args[0] + " was not found!");
                        }
                     
                    }
                    }
                    else if (args[0].equalsIgnoreCase("remove")) {
                        if (player.hasPermission("loj.remove")){
                        boolean playerFound1 = false; //sets the boolean to false because it has not found a player yet
                     
                        for (Player playerToRemove : Bukkit.getServer().getOnlinePlayers()) { //creates an array for the online players of the server
                            if(playerToRemove.getName().equalsIgnoreCase(args[1])) { //if a player was found matching args 1
                             
                                String uuid = playerToRemove.getUniqueId().toString();
                             
                                if (!config.contains("users." + uuid)) {
                                    player.sendMessage(message + ChatColor.YELLOW + "Player " + playerToRemove.getDisplayName() + ChatColor.YELLOW + " is not currently on the list!");
                                    playerFound1 = true;
                                    break;
                                 
                                }
                             
                                if (config.contains("users." + uuid)) {
                                    config.set("users." + uuid, null);
                                    saveConfig();
                                 
                                    playerToRemove.sendMessage(message + ChatColor.YELLOW + "You were removed from the Lightning on join list!");
                                    player.sendMessage(message + ChatColor.YELLOW + playerToRemove.getDisplayName() + ChatColor.YELLOW + " was successfully removed from the list successfully!");
                                    playerFound1 = true; //sets the boolean to true because it found a player
                                    break;
                                }
                             
                            }
                            }
                     
                        if (playerFound1 == false) {  //if the boolean was never set to true
                            player.sendMessage(message + ChatColor.RED + "Player " + args[0] + " was not found!");
                        }
                     
                    }
                    }
                } else {  //if the command did not have the correct amount of args
                    String message = ChatColor.BLACK + "[" + ChatColor.GOLD + "LightningOnJoin" + ChatColor.BLACK + "] ";
                 
                    player.sendMessage(message + ChatColor.RED + "Incorrect usage! Correct usage is: /loj <add/remove> <player>");
                }
             
                return true;
             
            }
         
            return false; 
         
        }
     
    }

    Listener Class -
    Code:
    package me.[REDACTED];
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class JoinListner implements Listener {
     
        LightningClass configGetter;
    
        public JoinListner(LightningClass plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            configGetter = plugin;
        }
     
        FileConfiguration config = configGetter.getConfig();
     
            @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
         
            String uuid = player.getUniqueId().toString();
                if (config.contains("users." + uuid)) {
                    System.out.println("User found on list");
                    player.getWorld().strikeLightning(player.getLocation());
                    player.setFireTicks(0);
            }
                while (player.getFireTicks() >= 1) {
                    player.setFireTicks(0);
                    player.setHealth(20);
                }
                if (config.contains("users." + uuid)) {
                    System.out.println("User is not on the list");
            }
        }
    }

    Error Log(took out what I think is garbage) -
    Code:
    [11:09:40 INFO]: This server is running CraftBukkit version git-Spigot-570526c-c1b529e (MC: 1.10.2) (Implementing API version 1.10.2-R0.1-SNAPSHOT)
    [11:09:40 INFO]: Set PluginClassLoader as parallel capable
    [11:09:40 INFO]: [LightningOnJoin] Loading LightningOnJoin v1.0
    [11:09:40 INFO]: **** Beginning UUID conversion, this may take A LONG time ****
    [11:09:42 INFO]: [LightningOnJoin] Enabling LightningOnJoin v1.0
    [11:09:42 ERROR]: Error occurred while enabling LightningOnJoin v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.[REDACTED].JoinListner.<init>(JoinListner.java:18) ~[?:?]
            at me.[REDACTED].LightningClass.onEnable(LightningClass.java:16) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot.jar:git-Spigot-570526c-c1b529e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-570526c-c1b529e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-570526c-c1b529e]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugin(CraftServer.java:373) [spigot.jar:git-Spigot-570526c-c1b529e]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot.jar:git-Spigot-570526c-c1b529e]
            at net.minecraft.server.v1_10_R1.MinecraftServer.t(MinecraftServer.java:412) [spigot.jar:git-Spigot-570526c-c1b529e]
            at net.minecraft.server.v1_10_R1.MinecraftServer.l(MinecraftServer.java:377) [spigot.jar:git-Spigot-570526c-c1b529e]
            at net.minecraft.server.v1_10_R1.MinecraftServer.a(MinecraftServer.java:332) [spigot.jar:git-Spigot-570526c-c1b529e]
            at net.minecraft.server.v1_10_R1.DedicatedServer.init(DedicatedServer.java:271) [spigot.jar:git-Spigot-570526c-c1b529e]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:535) [spigot.jar:git-Spigot-570526c-c1b529e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    [11:09:42 INFO]: Done (2.163s)! For help, type "help" or "?"
    Any help is greatly appreciated.

    EDIT: Name redacted.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TheDiamondCraver16
    FileConfiguration config = configGetter.getConfig();
    Gets called on a moment where configGetter is null.
     
  3. Offline

    timtower Administrator Administrator Moderator

  4. @timtower so how do I make it so it works in my instance? I don't know how to make it work
     
  5. Offline

    timtower Administrator Administrator Moderator

    @TheDiamondCraver16 Declare the variable like you are doing with configGetter, then set it in the constructor
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TheDiamondCraver16 You have a similar structure already:
    Code:
    LightningClass configGetter;
    
    public JoinListner(LightningClass plugin) {
    configGetter = plugin;
    }
     
  7. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page