Solved Getting a Null Pointer even though everything seems to work

Discussion in 'Spigot Plugin Development' started by BenPlaysMC, Jul 15, 2018.

Thread Status:
Not open for further replies.
  1. I'm making a mount plugin and in the middle of making a cooldown for the command and when I join and leave, in the console it throws an NPE but everything seems to work fine. The odd thing is that the section of the config that handles cooldown time seems to delete itself as well. The error is on line 27 and line 40 of the join and leave class.

    int cdtime = plugin.getConfig().getConfigurationSection("cooldown-time").getInt(uuid + ".Cooldown_Left");

    plugin.getConfig().getConfigurationSection("cooldown-time").set(uuid + ".Cooldown_Left",
    Commands.getCdtime().get(uuid));

    Code (Java):
    package eco.mounts.events;

    import java.util.UUID;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    import eco.mounts.CustomMounts;
    import eco.mounts.commands.Commands;

    public class JoinAndLeave implements Listener {

        CustomMounts plugin; // = CustomMounts.getPlugin(CustomMounts.class);

        public JoinAndLeave(CustomMounts plugin) {
            this.plugin = plugin;
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player plr = event.getPlayer();
            UUID uuid = plr.getUniqueId();

            int cdtime = plugin.getConfig().getConfigurationSection("cooldown-time").getInt(uuid + ".Cooldown_Left"); //ERROR HERE
            if (cdtime == 0) {
                return;
            } else {
                Commands.getCdtime().put(uuid, cdtime);
            }
        }

        @EventHandler
        public void onLeave(PlayerQuitEvent event) {
            Player plr = event.getPlayer();
            UUID uuid = plr.getUniqueId();

            plugin.getConfig().getConfigurationSection("cooldown-time").set(uuid + ".Cooldown_Left",
                    Commands.getCdtime().get(uuid)); //ERROR HERE ASWELL
            plugin.saveConfig();
            Commands.getCdtime().remove(uuid);
        }

    }
     
    Code (Java):
    package eco.mounts;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import eco.mounts.commands.Commands;
    import eco.mounts.events.CombustEvent;
    import eco.mounts.events.JoinAndLeave;
    import eco.mounts.events.onDismount;
    import eco.mounts.mounts.GiantZombieMount;
    import eco.mounts.mounts.HuskMount;
    import eco.mounts.mounts.IronGolemMount;
    import eco.mounts.mounts.SkeletonMount;
    import eco.mounts.mounts.ZombieMount;

    public class CustomMounts extends JavaPlugin implements Listener {
     
        private Commands commands = new Commands(this);

        public void onEnable() {
            getCommand(commands.cmd1).setExecutor(commands);
            Bukkit.getServer().getPluginManager().registerEvents(new onDismount(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new CombustEvent(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new JoinAndLeave(this), this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            commands.runnablerunner();
            new GiantZombieMount(this);
            new HuskMount(this);
            new IronGolemMount(this);
            new SkeletonMount(this);
            new ZombieMount(this);
        }

        public void onDisable() {
            saveConfig();
        }

    }
     
    Code (YAML):
    # CustomMounts Config File #

    # Default HP is 100
    # Cooldown is in minutes

    cooldown
    : 5

    zombie
    :
      hp
    : 100
      speed
    : 5
    skeleton
    :
      hp
    : 100
      speed
    : 5
    iron_golem
    :
      hp
    : 100
      speed
    : 5
    husk
    :
      hp
    : 100
      speed
    : 5
    giant_zombie
    :
      hp
    : 100
      speed
    : 5
     
    # USERS WITH COOLDOWN TIME LEFT #

    cooldown-time:
     
    #1 BenPlaysMC, Jul 15, 2018
    Last edited: Jul 15, 2018
  2. can u tell where is the actual line 27 and line 40, like copy them out, this suck enjin not gona show us the line :L
     
  3. Alright, I just copied them out.
     
  4. why are u using something like
    getConfigurationSection("cooldown-time").set(uuid + ".Cooldown_Left", Commands.getCdtime().get(uuid))
    it should be
    Code (Text):

    .set("cooldown-time." + uuid + ".Cooldown_Left", Commands.getCdtime().get(uuid))
     
     
    • Like Like x 1
  5. Use
    Code (Java):

    plugin.getConfig().getInt("cooldown-time." + uuid + ".Cooldown_Left");
     
    instead of
    Code (Java):

    plugin.getConfig().getConfigurationSection("cooldown-time").getInt(uuid + ".Cooldown_Left");
     
    EDIT:
    also change:
    Code (Java):

      if (cdtime == 0) {
                return;
            } else {
                Commands.getCdtime().put(uuid, cdtime);
            }
     
    to
    Code (Java):

    if (cdtime != 0) {
                Commands.getCdtime().put(uuid, cdtime);
            }
     
    It's neater.
     
    • Like Like x 1
  6. Thanks, guys for the help!
     
Thread Status:
Not open for further replies.