Problem with methods [Please Help]

Discussion in 'Plugin Development' started by Dagovrek222, May 15, 2016.

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

    Dagovrek222

    Hey,

    I got a problem with methods in my plugin.
    This are the parts of code which are important:

    This part of code is from the start class
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        
            if (cmd.getName().equalsIgnoreCase("lbintialize") && sender.hasPermission("lbsk.intialize")) {
            
                if (sender instanceof Player){
                    Player player = (Player) sender;
                    World world = player.getWorld();
                    List<String> playerlist = new ArrayList<String>();
                    for (Player all:getServer().getOnlinePlayers()){
                        playerlist.add(all.getName());
                        
                        }
                    FireEvent.onIntialize(playerlist);
                    }
                
                }
            
        
        
            return true;
        }
    this part of code is inside an event package in a class called FireEvent:
    Code:
    public class FireEvent implements Listener {
    
        private Map<String, Long> sniper;
        private Map<String, Long> regen;
        private Map<String, Long> tnt;
        private Map<String, Long> boost;
        private Map<String, Long> ln;
        private Map<Integer, String> el;
        private Map<String, Long> inv;
    
        public FireEvent() {
            this.sniper = new HashMap<>();
            this.regen = new HashMap<>();
            this.tnt = new HashMap<>();
            this.boost = new HashMap<>();
            this.ln = new HashMap<>();
            this.el = new HashMap<>();
            this.inv = new HashMap<>();
        }
    
    public void onIntialize(List<String> playerlist) {
            for (int i=0; i<playerlist.size(); i++) {
                String player = playerlist.get(i).toString();
                sniper.put(player, System.currentTimeMillis());
                regen.put(player, System.currentTimeMillis());
                tnt.put(player, System.currentTimeMillis());
                boost.put(player, System.currentTimeMillis());
                ln.put(player, System.currentTimeMillis());
                inv.put(player, System.currentTimeMillis());
            }
        
        }
    My problem is that I have to make the method onIntialize() a static method to be able to call it, but when I make it a static method, I cant add things to my private hashmaps.

    Does anyone know how I might be able to fix this?

    The error I get is in the image.


    EDIT:
    I think I made an instance. I added this line to the FireEvent class inside the events package:
    Code:
    public static FireEvent fire;
        public void onEnable() {
            fire = this;
           
        }
       
    and I call the method from the start class in the normal package with this:
    Code:
    FireEvent.fire.onIntialize(playerlist);
    but when executing it I get an error which looks like this:
    Click to show error (open)

    ... 15 more at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-3f3c65f] at me.Dagovrek222.Wand.Start.onCommand(Start.java:41) ~[?:?]Caused by: java.lang.NullPointerException at java.lang.Thread.run(Unknown Source) [?:1.8.0_91] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-3f3c65f] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-3f3c65f] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-3f3c65f] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-3f3c65f] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-3f3c65f] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-3f3c65f]org.bukkit.command.CommandException: Unhandled exception executing command 'lbintialize' in plugin Wand v1.0[19:48:34 ERROR]: null


    The error occurs on line 41 which is the line where I call the method. I hope anyone knows how to fix it.
     

    Attached Files:

    Last edited: May 16, 2016
  2. Offline

    timtower Administrator Administrator Moderator

    @Dagovrek222 Make an instance out of it first if you don't do that already, if you do that: then keep track of the instance.
     
  3. Offline

    Dagovrek222

    @timtower
    Thank you, but I don't have very much experience with java. Could you please explain me a little bit how I have to do that?
     
  4. Offline

    glassbillen

    Code feeeding is wrong. I'll do it for this time but you will need to learn proper java if you want to succéd in the future.

    FireEvent fe;
    fe = new FireEvent();
    fe.onIntialize(playerlist);
    That should work :p
     
    Dagovrek222 likes this.
Thread Status:
Not open for further replies.

Share This Page