Help with null pointer exception

Discussion in 'Plugin Development' started by bowlerguy66, Jul 19, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    bowlerguy66

    Hello. I have made a command that runs through 84 numbers then goes to standby mode. Everything seems fine but when I do the command, this error spams my console:

    Code:
    [10:58:13 WARN]: Exception in thread "Craft Scheduler Thread - 58"
    [10:58:13 WARN]: org.apache.commons.lang.UnhandledException: Plugin LegacyCells v1.0 generated an exception while executing task 100
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NullPointerException
        at me.bowlerguy66.legacycells.LegacyCells.dClThru(LegacyCells.java:108)
        at me.bowlerguy66.legacycells.LegacyCells$1.run(LegacyCells.java:89)
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more
    This is the command

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                   
            if(label.equalsIgnoreCase("test")) {
    
                dclnum = 84;
                timerCheck();
                return true;
               
            }
               
                return false;
           
        }
       
        @SuppressWarnings("deprecation")
        public void timerCheck() {
           
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                    if(smnumber != -1){
                        if(smnumber != 0) {
                            smnumber -= 0.000000000001;
                        } else {
                            smnumber = 0.000000000001;
                            dClThru();
                        }
                    }
                }
               
            }, 0L, 0L);   
        }
       
        // 84
       
        public void dClThru() {
           
            if(dclnum == 0) {
               
                Bukkit.broadcastMessage("Standby!");
                return;
               
            } else {
               
                if(getConfig().get("Cells." + "D0" + dclnum).equals(null)) {
                    Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " does not exist.");
                    dclnum--;
                    return;
                }
               
                if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(0)) {
                    Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:0!");
                    dclnum--;
                    return;
                } else if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(1)) {
                    Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:1!");
                    dclnum--;
                    return;
                }
    
            }
           
        }
        
     
  2. Offline

    Zombie_Striker

  3. Offline

    bowlerguy66

  4. Offline

    Zombie_Striker

    @bowlerguy66
    Then you should not be coding. Not being able to read the error logs (its only two lines you have to look for) is something every developer must know how to do.

    This is the line. Read your log to figure out what the problem is, and fix it.
     
  5. Offline

    bowlerguy66

    :/ I did, I have been looking at it for about the past 2 and a half hours looking for a fix. Thats why I came here
     
  6. Offline

    Zombie_Striker

    @bowlerguy66
    Fine, I'll make it easier for you:
    So you can see that it says line 108 of LegacyCells.class if null.

    If getConfig() null? Is the thing you are getting null (.equals would cause the problem if it is null. Use == if you're not sure if something is null)
     
  7. @bowlerguy66
    Yeah, if you use .equals(null) you either get true or a NullPointerException. A null check is to prevent an NPE on using methods in an object. Use == instead, since that won't give you an NPE.
     
  8. Offline

    bowlerguy66

    Ok, I did that now It says the things that the things in the config don't exist. Any suggestions?

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                   
            if(label.equalsIgnoreCase("test")) {
    
                dclnum = 84;
                timerCheck();
                return true;
               
            }
               
                return false;
           
        }
       
        @SuppressWarnings("deprecation")
        public void timerCheck() {
           
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                    if(smnumber != -1){
                        if(smnumber != 0) {
                            smnumber -= 0.000000000001;
                        } else {
                            smnumber = 0.000000000001;
                            dClThru();
                        }
                    }
                }
               
            }, 0L, 0L);   
        }
       
        // 84
       
        public void dClThru() {
           
            if(dclnum == 0) {
               
                Bukkit.broadcastMessage("Standby!");
                return;
               
            } else {
               
                if(!getConfig().contains("Cells." + "D0" + dclnum)) {
                    Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " does not exist.");
                    dclnum--;
                    return;
                } else {
               
                    if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(0)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:0!");
                        dclnum--;
                        return;
                    } else if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(1)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:1!");
                        dclnum--;
                        return;
                    } else {
                        Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " had an error");
                        dclnum--;
                        return;
                    }
                }
            }
           
        }
        
     
  9. Offline

    Zombie_Striker

  10. Offline

    bowlerguy66

    Oh okay

    :/ Still not working. Anything else?

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                   
            if(label.equalsIgnoreCase("test")) {
    
                dclnum = 84;
                timerCheck();
                return true;
               
            }
               
                return false;
           
        }
       
        @SuppressWarnings("deprecation")
        public void timerCheck() {
           
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                    if(smnumber != -1){
                        if(smnumber != 0) {
                            smnumber -= 0.000000000001;
                        } else {
                            smnumber = 0.000000000001;
                            dClThru();
                        }
                    }
                }
               
            }, 0L, 0L);   
        }
       
        // 84
       
        public void dClThru() {
           
            if(dclnum == 0) {
               
                Bukkit.broadcastMessage("Standby!");
                return;
               
            } else {
               
                if(getConfig().get("Cells." + "D0" + dclnum) == null) {
                    saveConfig();
                }
               
                if(!getConfig().contains("Cells." + "D0" + dclnum)) {
                    Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " does not exist.");
                    saveConfig();
                    dclnum--;
                    return;
                } else {
               
                    if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(0)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:0!");
                        dclnum--;
                        return;
                    } else if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(1)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:1!");
                        dclnum--;
                        return;
                    } else {
                        Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " had an error");
                        dclnum--;
                        return;
                    }
                }
            }
           
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  11. Offline

    Joserex65

    Would help that you post the whole class. However, if you don't want to you have errors on lines 89 and 108.
     
  12. Offline

    bowlerguy66

    Code:
    package me.bowlerguy66.legacycells;
    
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    
    public class LegacyCells extends JavaPlugin {
    
        public final BukkitListener bl = new BukkitListener(this);
        public double smnumber = 0.000000000001;
        public int number = 100;
        public int dclnum = 84;
        public static Economy econ = null;
       
        private boolean setupEconomy() {
           
             RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                econ = economyProvider.getProvider();
            }
    
            return (econ != null);
        }
       
        public WorldGuardPlugin getWorldGuard() {
           
            Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
           
            if ((plugin == null) || (!(plugin instanceof WorldGuardPlugin))) {
               
                return null;
           
            }
           
            return (WorldGuardPlugin) plugin;
           
        }
       
        public void onEnable() {
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(bl, this);
            getWorldGuard();
           
            if(!setupEconomy()) {
                getLogger().severe("Plugin requieres vault!");
                Bukkit.getPluginManager().disablePlugin(this);
            }
           
        }
       
        public void onDisable() {
    
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                   
            if(label.equalsIgnoreCase("test")) {
    
                dclnum = 84;
                timerCheck();
                return true;
               
            }
               
                return false;
           
        }
       
        @SuppressWarnings("deprecation")
        public void timerCheck() {
           
            this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                    if(smnumber != -1){
                        if(smnumber != 0) {
                            smnumber -= 0.000000000001;
                        } else {
                            smnumber = 0.000000000001;
                            dClThru();
                        }
                    }
                }
               
            }, 0L, 0L);   
        }
       
        // 84
       
        public void dClThru() {
           
            if(dclnum == 0) {
               
                Bukkit.broadcastMessage("Standby!");
                return;
               
            } else {
               
                if(getConfig().get("Cells." + "D0" + dclnum) == null) {
                    saveConfig();
                }
               
                if(!getConfig().contains("Cells." + "D0" + dclnum)) {
                    Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " does not exist.");
                    saveConfig();
                    dclnum--;
                    return;
                } else {
               
                    if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(0)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:0!");
                        dclnum--;
                        return;
                    } else if(getConfig().get("Cells." + "D0" + dclnum + ".Type").equals(1)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "D0" + dclnum + " was Type:1!");
                        dclnum--;
                        return;
                    } else {
                        Bukkit.broadcastMessage(ChatColor.RED + "D0" + dclnum + " had an error");
                        dclnum--;
                        return;
                    }
                }
            }
           
        }
       
    }
     
  13. Offline

    Zombie_Striker

     
Thread Status:
Not open for further replies.

Share This Page