Solved Could not pass event VotifierEvent to BitsecVotifier

Discussion in 'Spigot Plugin Help' started by RamonRobben, Jun 14, 2018.

  1. Hi, I am trying to create a votifier plugin. and now I have followed a tutorial on how to make a config file to save the votes in. The only problem is that I get this error: Could not pass event VotifierEvent to BitsecVotifier

    Code (Text):
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at com.vexsoftware.votifier.net.VoteReceiver$1.run(VoteReceiver.java:182) [votifier-1.9.jar:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:71) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.NullPointerException
            at com.bitsec.Votifier.VoteEvent.onVotifierEvent(VoteEvent.java:33) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-3d850ec-809c399]
            ... 11 more
    This is my Main class: Votifier.java

    Code (Java):
    package com.bitsec.Votifier;

    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Votifier extends JavaPlugin {
     
        private ConfigManager cfgm;
     
        public void onEnable() {
            loadConfigManager();
            getServer().getPluginManager().registerEvents(new VoteEvent(), this);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[BitsecVotifier] has been enabled!");
        }
     
        public void loadConfigManager() {
            cfgm = new ConfigManager();  
            cfgm.setup();
        }
     
        public void onDisable() {
            cfgm.votescfg.addDefault("players", "player");
            cfgm.votescfg.options().copyDefaults(true);
            saveConfig();
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "[BitsecVotifier] has been disabled!");
        }
     
    }
     
    and here is my event listener class: VoteEvent.java

    Code (Java):
    package com.bitsec.Votifier;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;

    import com.vexsoftware.votifier.model.Vote;
    import com.vexsoftware.votifier.model.VotifierEvent;

    import net.md_5.bungee.api.ChatColor;

    public class VoteEvent implements Listener {
     
        private ConfigManager cfgm;
     
        @EventHandler(priority=EventPriority.NORMAL)
        public void onVotifierEvent(VotifierEvent event) {

            Vote v = event.getVote();
            Player p = Bukkit.getPlayer(v.getUsername());
         
            if (p == null) {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[BitsecVotifier] " + v.getUsername() + ChatColor.RED + " [Offline]" + ChatColor.GREEN + " has voted on " + v.getServiceName());
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[BitsecVotifier] Redeem your offline votes using /redeemvotes");
                int offlinevotes = cfgm.getVotes().getInt("players." + v.getUsername() + ".offlinevotes");
                cfgm.getVotes().set("players." + v.getUsername() + ".offlinevotes", offlinevotes++);
            }else {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[BitsecVotifier] " + v.getUsername() + " has voted on " + v.getServiceName());
                int votes = cfgm.getVotes().getInt("players." + v.getUsername() + ".votes");
                cfgm.getVotes().set("players.RamonRobben.votes", votes++);
            }
         
            cfgm.saveVotes();
         
        }
    }
     
    And the ConfigManager class just incase you need it:

    Code (Java):
    package com.bitsec.Votifier;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import net.md_5.bungee.api.ChatColor;

    public class ConfigManager {

        private Votifier plugin = Votifier.getPlugin(Votifier.class);
     
        //Files & File configs here
        public FileConfiguration votescfg;
        public File votesfile;
        //...........................
     
        public void setup() {
            //check if we have datafolder otherwise make one
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
            }
         
            votesfile = new File(plugin.getDataFolder(), "voters.yml");
         
            if (!votesfile.exists()) {
                try {
                    votesfile.createNewFile();
                    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[BitsecVotifier] voters.yml file has been created");
                }catch(Exception e) {
                    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "[BitsecVotifier] Could not create voters.yml file");
                }
            }
         
            this.reloadVotes();  
        }
     
        public FileConfiguration getVotes() {
            return votescfg;
        }
     
        public void saveVotes() {
            try {
                votescfg.save(votesfile);
            } catch (Exception e) {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "[BitsecVotifier] voters.yml coul not be saved");
            }
        }
     
        public void reloadVotes() {
            votescfg = YamlConfiguration.loadConfiguration(votesfile);
        }
     
    }
     
    I would like to know what I am doing wrong.
     
  2. I am pretty sure your ConfigManager is never defined in your VoteEvent class, as you have this in the top section of your VoteEvent class:
    Code (Text):
    private ConfigManager cfgm;
    But you never set this cfgm object to anything. It remains 'null', and thus caused the NullPointer Exception at line 33 of your VoteEvent class.

    How to fix this:
    In your onEnable, change this line
    Code (Text):
    getServer().getPluginManager().registerEvents(new VoteEvent(), this);
    to this:
    Code (Text):
    getServer().getPluginManager().registerEvents(new VoteEvent(cfgm), this);
    Then, in your VoteEvent class, add the following code on the top to define your cfgm object in there:
    Code (Text):
    public VoteEvent(ConfigManager cfgm){
       this.cfgm = cfgm;
    }
     
  3. Thank you for your answer @JustDJplease
    I now see the similarities with C# and Java and now I understand why and how this error occured and could be fixed.
    I saw some of this code before but couldn't figure out how to implement it as when trying to give cfgm as parameter it wouldn't work.
    That was because I didn't have the constructor that sets this.cfgm to the cfgm passed onto it.

    Anyways thanks.
     
    • Friendly Friendly x 1
  4. Awesome! You can change the prefix of your thread to [Solved] so everyone else will know that you're no longer looking for an answer!