Plugin Help

Discussion in 'Spigot Plugin Development' started by Angeles, May 26, 2016.

  1. [20:45:41 ERROR]: Error occurred while enabling VitalCore v1.0 (Is it up to date?)
    java.lang.NullPointerException
    at com.vitalhcf.main.Main.onEnable(Main.java:102) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot(2).jar:git-Spigot-1649]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot(2).jar:git-Spigot-1649]
     
  2. show me error code line (Main.java:102)
     
  3. enderPearl.init();
     
  4. show me the enderPearl code
     
  5. Code (Text):
    package com.vitalhcf.scoreboard;

    import java.util.HashMap;
    import java.util.Map;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event.Result;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;

    import com.vitalhcf.main.Main;

    public class EnderPearlManager implements Listener{

        private Map<Player , Long> enderpearlCooldownMap= new HashMap<Player , Long>();

        private Main plugin;
        private int enderpearlCoolDown;

        public EnderPearlManager(Main plugin) {
            this.plugin = plugin;
        }

        public void init(){
            enderpearlCoolDown = plugin.getConfig().getInt("SCOREBOARD.SETTINGS.COOLDOWNS.ENDERPEARL_COOLDOWN");
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }

        public boolean isCooldownActive(Player player){
            if(!enderpearlCooldownMap.containsKey(player)){
                return false;
            }
            long value = enderpearlCooldownMap.get(player);
            return value > System.currentTimeMillis();
        }

        public long getMillisecondLeft(Player player){
            if(!isCooldownActive(player)){
                return -1;
            }
            return enderpearlCooldownMap.get(player) - System.currentTimeMillis();
        }

        @EventHandler(priority = EventPriority.MONITOR )
        public void onPlayerInteract(PlayerInteractEvent event){
            ItemStack item = event.getItem();
            if(item != null && item.getType() == Material.ENDER_PEARL && (event.getAction() == Action.RIGHT_CLICK_AIR )){
                Player player = event.getPlayer();
                if(isCooldownActive(player)){
                    event.setUseItemInHand(Result.DENY);
                }else{
                    long until = System.currentTimeMillis() + enderpearlCoolDown * 1000L;
                    enderpearlCooldownMap.put(player, until);
                }
            }
        }

        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event){
            enderpearlCooldownMap.remove(event.getPlayer());
        }
    }
     
     
  6. You are getting a null pointer exception. The only reason that the line would throw that is if enderPearl is null. So take a look at your Main class and see why enderPearl would not be initialized.
     
    • Agree Agree x 1
  7. My main class

    Code (Text):
    package com.vitalhcf.main;

    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import com.vitalhcf.commands.ClearChatCommand;
    import com.vitalhcf.scoreboard.ArmorClassWrapper;
    import com.vitalhcf.scoreboard.CombatTagWrapper;
    import com.vitalhcf.scoreboard.DTCWrapper;
    import com.vitalhcf.scoreboard.EnderPearlManager;
    import com.vitalhcf.scoreboard.FactionWrapper;
    import com.vitalhcf.scoreboard.KothWrapper;
    import com.vitalhcf.scoreboard.PvPTimerWrapper;
    import com.vitalhcf.utils.MessageManager;
    import com.vitalhcf.utils.YmlMaker;

    import net.minecraft.server.v1_7_R4.Scoreboard;
    import net.minecraft.util.org.apache.commons.lang3.StringEscapeUtils;
    import net.minecraft.util.org.apache.commons.lang3.text.WordUtils;

    /**
    * Created by Jason on 07/03/2016.
    */
    public class Main extends JavaPlugin implements Listener {

        PluginManager pm = Bukkit.getServer().getPluginManager();
        public static Main instance;
        public static Plugin pl;
        public static YmlMaker messages;

        private DTCWrapper dtcWrapper;
        private KothWrapper kothWrapper;
        private FactionWrapper factionWrapper;
        private EnderPearlManager enderPearl;
        private ArmorClassWrapper armor;
        private CombatTagWrapper combatTag;
        private PvPTimerWrapper pvptimer;
        private DecimalFormat formatter = new DecimalFormat("0.0");
        private List<String> base;
        private String bars;
        private String title;
        private Map<Player , ScoreboardHelper> scoreboardHelperMap = new HashMap<>();
       
        private MessageManager mm = getMessageManager();
        private static MessageManager messageManager;

        public static YmlMaker fixes;
        public static YmlMaker lang;

        public void onEnable() {
            messages = new YmlMaker(this, "messages.yml");
            messages.saveDefaultConfig();

            saveDefaultConfig();

            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            instance = this;
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "      HCFCore coded by Lapping     ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "               Enabled             ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");;

            super.getCommand("cc").setExecutor(new ClearChatCommand());

            //Listeners

           
        //Start of Scoreboard Code
            dtcWrapper = new DTCWrapper();
            kothWrapper = new KothWrapper();
            factionWrapper = new FactionWrapper(this);
            EnderPearlManager enderClearPearl = new EnderPearlManager(this);
            armor = new ArmorClassWrapper();
            combatTag = new CombatTagWrapper(this);
            pvptimer = new PvPTimerWrapper();
            getConfig().options().copyDefaults();
            base = translate(getConfig().getStringList("SCOREBOARD.LINES"));
            bars = translate(getConfig().getString("SCOREBOARD.SETTINGS.BARS"));
            title = translate(getConfig().getString("SCOREBOARD.TITLE"));
            dtcWrapper.init();
            kothWrapper.init();
            factionWrapper.init();
            enderPearl.init();
            armor.init();
            combatTag.init();
            pvptimer.init();


            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    for(Map.Entry<Player , ScoreboardHelper> entry : scoreboardHelperMap.entrySet())
                    {
                        Player player = entry.getKey();
                        ScoreboardHelper helper = entry.getValue();
                        helper.clear();
                        boolean addedSomethingButBars = false;
                        {
                            for(String string : base)
                            {
                                boolean skip = false;
                                if(string.contains("%bars%"))
                                {
                                    string = string.replace("%bars%", bars);
                                }
                                else
                                {
                                    if(string.contains("%pvptime%"))
                                    {
                                        if(pvptimer.isPVPTimerActive(player))
                                        {
                                            int data = pvptimer.getPVPTimerActive(player);
                                            int minutes = (int) (data / 60.0D);
                                            int seconds = data % 60;
                                            String str = String.format("%d:%02d", Integer.valueOf(minutes), Integer.valueOf(seconds) );
                                            string = string.replace("%pvptime%", str);
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    if(string.contains("%active_class%"))
                                    {
                                        if(armor.isAnyActive(player))
                                        {
                                            string = string.replace("%active_class%", String.valueOf(armor.getActiveKit(player)));
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    else if(string.contains("%energy%"))
                                    {
                                        if(armor.isBardActive(player))
                                        {
                                            string = string.replace("%energy%", getDecimalFormatter().format(armor.getBardPower(player)));
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    if(string.contains("%kothname%") || string.contains("%kothtime%") ) {
                                        List<String> input = kothWrapper.processKothString(string);
                                        for (String i : input) {
                                            helper.add(i);
                                        }
                                        skip = true;
                                        if (!input.isEmpty()) {
                                            addedSomethingButBars = true;
                                        }
                                    }
                                    if(string.contains("%combat%"))
                                    {
                                        if(combatTag.isCooldownActive(player))
                                        {
                                            long left = combatTag.getMillisecondLeft(player);
                                            string = string.replace("%combat%", String.valueOf(getDecimalFormatter().format(left / 1000.0D)));
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    if(string.contains("%dtcname%"))
                                    {
                                        if(dtcWrapper.isDTCActive())
                                        {
                                            string = string.replace("%dtcname%", dtcWrapper.getActiveDTC());
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    if(string.contains("%dtcblocks%"))
                                    {
                                        if(dtcWrapper.isDTCActive())
                                        {
                                            string = string.replace("%dtcblocks%", String.valueOf(dtcWrapper.getDTCPointLeft()));
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }

                                    if(string.contains("%enderpearl%"))
                                    {
                                        if(enderPearl.isCooldownActive(player))
                                        {
                                            long left = enderPearl.getMillisecondLeft(player);
                                            string = string.replace("%enderpearl%", String.valueOf(getDecimalFormatter().format(left / 1000.0D)));
                                        }
                                        else
                                        {
                                            skip = true;
                                        }
                                    }
                                    if (string.contains("%title%"))
                                    {
                                        string = string.replace("%title%", translate("&9&lStaffMode&7:"));
                                    }

                                    if (string.contains("%check%"))
                                    {
                                        if (player.isOp())
                                        {
                                            string = string.replace("%check%", translate(" &6» &9Gamemode&7: &c" + WordUtils.capitalizeFully(player.getGameMode().name())));
                                        }
                                        else
                                        {
                                            if (player.isFlying())
                                            {
                                                string = string.replace("%check%", translate(" &6» &9Fly&7: &aTrue"));
                                            }
                                            else
                                            {
                                                string = string.replace("%check%", translate(" &6» &9Fly&7: &cFalse"));
                                            }
                                        }
                                    }

                                    if(!skip)
                                    {
                                        addedSomethingButBars = true;
                                    }
                                }
                                if(!skip)
                                {
                                    helper.add(string);
                                }
                            }
                        }
                        if(!addedSomethingButBars)
                        {
                            helper.clear();
                        }
                        helper.update(player);
                    }
                }
            }.runTaskTimer(this, 1, 1);
            for(Player player : Bukkit.getServer().getOnlinePlayers())
            {
                onJoin(player);
            }
        }

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            onJoin(event.getPlayer());
        }

        public void onJoin(final Player player)
        {
            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    if(player.isOnline())
                    {
                        Scoreboard scoreboard = (Scoreboard) Bukkit.getScoreboardManager().getNewScoreboard();
                        ScoreboardHelper scoreboardHelper = new ScoreboardHelper((org.bukkit.scoreboard.Scoreboard) scoreboard, title);
                        scoreboardHelperMap.put(player, scoreboardHelper);
                    }
                }
            }.runTaskLater(this, 20);
        }

        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event)
        {
            Player player = event.getPlayer();
            scoreboardHelperMap.remove(player);
        }

        public DecimalFormat getDecimalFormatter()
        {
            return formatter;
        }

        public String translate(String input)
        {
            return StringEscapeUtils.unescapeJava(ChatColor.translateAlternateColorCodes('&', input));
        }

        public List<String> translate(List<String> input)
        {
            List<String> text = new ArrayList<>();
            for(String string : input)
            {
                text.add(translate(string));
            }
            return text;
        }

        // End of scoreboard Code

        public FactionWrapper getFaction()
        {
            return factionWrapper;
        }


        public void onDisable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "      HCFCore coded by Lapping     ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "               Disabled            ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");
        }

        public YmlMaker messages()
        {
            return messages;
        }
       
       
        public static MessageManager getMessageManager()
        {
          return messageManager;
        }


        public static Main getInstance()
        {
            return instance;
        }
    }

     
     
  8. Code (Text):
    EnderPearlManager enderClearPearl = new EnderPearlManager(this);
    enderPearl.init();
    You Have not instantiated enderPearl but you have instantiated enderClearPearl, If I was to guess you might have just typed in the wrong name.
    Try changing enderPearl.init() to enderClearPearl.init()
     
    • Agree Agree x 1
  9. I wanted you to take a look at the class. Null pointers (especially like this one) aren't too tricky to debug and it will be a lot easier for you in the future if you learn to debug them yourself so that you don't have to take the time to post and wait for a reply on here.

    The only code being run before you call the line throwing the exception is this:
    Code (Text):
    PluginManager pm = Bukkit.getServer().getPluginManager();
        public static Main instance;
        public static Plugin pl;
        public static YmlMaker messages;

        private DTCWrapper dtcWrapper;
        private KothWrapper kothWrapper;
        private FactionWrapper factionWrapper;
        private EnderPearlManager enderPearl;
        private ArmorClassWrapper armor;
        private CombatTagWrapper combatTag;
        private PvPTimerWrapper pvptimer;
        private DecimalFormat formatter = new DecimalFormat("0.0");
        private List<String> base;
        private String bars;
        private String title;
        private Map<Player , ScoreboardHelper> scoreboardHelperMap = new HashMap<>();
       
        private MessageManager mm = getMessageManager();
        private static MessageManager messageManager;

        public static YmlMaker fixes;
        public static YmlMaker lang;

        public void onEnable() {
            messages = new YmlMaker(this, "messages.yml");
            messages.saveDefaultConfig();

            saveDefaultConfig();

            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            instance = this;
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "      HCFCore coded by Lapping     ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "               Enabled             ");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "");
            getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "----------------------------------");;

            super.getCommand("cc").setExecutor(new ClearChatCommand());

            //Listeners

           
        //Start of Scoreboard Code
            dtcWrapper = new DTCWrapper();
            kothWrapper = new KothWrapper();
            factionWrapper = new FactionWrapper(this);
            EnderPearlManager enderClearPearl = new EnderPearlManager(this);
            armor = new ArmorClassWrapper();
            combatTag = new CombatTagWrapper(this);
            pvptimer = new PvPTimerWrapper();
            getConfig().options().copyDefaults();
            base = translate(getConfig().getStringList("SCOREBOARD.LINES"));
            bars = translate(getConfig().getString("SCOREBOARD.SETTINGS.BARS"));
            title = translate(getConfig().getString("SCOREBOARD.TITLE"));
            dtcWrapper.init();
            kothWrapper.init();
            factionWrapper.init();
    Now we can scrap all of the code that isn't relevant to the problem at hand:
    Code (Text):
    private EnderPearlManager enderPearl;
    public void onEnable() {
        [...]
        EnderPearlManager enderClearPearl = new EnderPearlManager(this);
        [...]
        enderPearl.init()
    }
     
     
  10.  
  11. D
    doesnt work
     
  12. could you help
     
  13. I'm sorry but I didn't even give you a solution so I'm not sure what "doesnt work". I wanted to help you with debugging to prevent another post such as the one you just posted here
    You need to put some effort in here. You can't just post every error you run into without at least trying to debug it yourself!
     
    #13 MrBlobman, May 26, 2016
    Last edited: May 26, 2016
  14. i have treid alot and i dont know how to fix it dont tell me i dont put effort in
     
    • Optimistic Optimistic x 1

  15. So can anyone help
     
  16. Choco

    Moderator

    Please learn to read stackstraces >.< This is just getting stupid.

    "java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.scoreboard.CraftScoreboard cannot be cast to net.minecraft.server.v1_7_R4.Scoreboard"

    Your issue is literally written in English for you in the stacktrace... with the line underneath telling you WHERE the issue is occurring. We are allowed to tell you to put effort in because you're clearly not trying at all. Learn to read stacktraces, learn to write Java, and learn to not be ignorant to the people attempting to assist you
     

  17. Well im sorry i dont understand how to fix

    java.lang.NullPointerException
    at com.vitalhcf.main.Main.onEnable(Main.java:102) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot(2).jar:git-Spigot-1649]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot(2).jar:git-Spigot-1649]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot(2).jar:git-Spigot-1649]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot(2).jar:git-Spigot-1649]
     
  18. Choco

    Moderator

    This...
    Was never initialized... hence... null...
    You've been told a few times now that was the issue
     
  19. Please do not come here without learning basic java first (we cannot help you if you are unable to understand what anyone is talking about). This is an extremely simple problem with an obvious answer.

    You need to learn about OOP and how to create and use objects.

    java.lang.NullPointerException
    at com.vitalhcf.main.Main.onEnable(Main.java:102) ~[?:?]

    Very common error, it means you are trying to use something at line 102 of Main which does not exist, because you never created an instance of it.
     
    • Winner Winner x 2
  20. What so i would do public static [LINE 102]