Solved Calling functions from other classes

Discussion in 'Plugin Development' started by UNC00KED, Dec 25, 2015.

Thread Status:
Not open for further replies.
  1. What's the problem?

    Error:
    Code:
    [20:54:57] [Server thread/INFO]: UNC00KED issued server command: /test
    [20:54:57] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin OreRunner v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
        at me.unc00ked.OreRunner.onCommand(OreRunner.java:1283) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        ... 15 more
    [20:55:17]
    Line 1283 of the OreRunner (main class) is:
    Code:
    LeaderboardFunctions.plugin.test(player);

    Main Class:
    Code:
    public class OreRunner extends JavaPlugin {
       
        Logger logger = Logger.getLogger("Minecraft");
       
        public static OreRunner plugin;
       
        @Override
        public void onEnable() {
            logger.info("OreRunner Enabled!");
            new LeaderboardFunctions();
            plugin = this;
        }
       
        @Override
        public void onDisable() {
            logger.info("OreRunner Disabled!");
            plugin = null;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("test") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                LeaderboardFunctions.plugin.test(player);
               
                return true;
               
            }
           
            return false;
    
        }
    
    }

    LeaderboardFunctions Class:
    Code:
    public class LeaderboardFunctions extends JavaPlugin {
       
        public static LeaderboardFunctions plugin;
       
        void test(Player player) {
           
            player.sendMessage("Your name is: " + player.getName());
           
        }
       
    }
     
  2. Offline

    Zombie_Striker

    Please don't steal minecraft's logger. Use getLogger() first off. Secondly, bukkit already logs your plugins for you. You do not need to log it twice.

    Something on this line is null. BTW: plugin is null, and you are abusing static. More importanltly, use getters and setters for variables like this. Don't just grab the "plugin" instance from a field.
    You do not need to do this. The collectors will take care of those instances.

    Please don''t store an instance of a class inside that class.
     
    mine-care likes this.
  3. Soo... how do I fix my problem of accessing functions inside another class? Do I have to make every function static?
     
  4. Offline

    mcdorli

    Please learn java. Getters and setters are really very bsic
     
    teej107 likes this.
  5. No, you do not need to make every function static... Don't do that please.
    In your OreRunner, you create a new LeaderboardFunctions class. You want to save that instance in a field in your OreRunner class, then you can use that to call the method. You want to remove the static field in your LeaderboardFunctions class. The way you're doing it is bad practice :)
    Also, the reason you're getting the NPE, is because you never set LeaderboardFunctions.plugin. You're setting OreRunner#plugin.

    @Zombie_Striker
    It is actually valid to store an instance of a class inside that very class :p It's used in the Singleton design pattern.

    @mcdorli
    You're right, but it's kinda besides the point here. Getting and setting isn't the issue in this code. The issue is he's not setting the variable that he's referencing :)
     
  6. Offline

    Zombie_Striker

    @BreezerFly
    Yes, it's valid for a Skeleton, but not for what he's using here.
     
  7. For a singleton, what do you mean "skeleton"??

    @Zombie_Striker Ahh, but it makes no sense having a particular implementation in a Skeleton class. So having a static reference to an object there doesn't make much sense anyway, staying on topic!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 26, 2017
  8. Offline

    Zombie_Striker

  9. How would you recommend doing that? @BreezerFly
     
  10. By doing what I wrote...
     
  11. So do you want me to just add:
    Code:
    public static LeaderboardFunctions plugin2;
    to the top of my main class?
     
  12. You have to get rid of all static in your classes.
    So you have private LeaderboardFunctions lbFunc = new LeaderboardFunctions(); in your main class.

    Read some basic Java tutorials :)
     
    Zombie_Striker likes this.
  13. Well once I add that line of code in:
    Code:
    private LeaderboardFunctions lbFunc = new LeaderboardFunctions();
    my plugin isn't even loading...
     
  14. Offline

    Zombie_Striker

    @UNC00KED
    any errors? Maybe it's because you created an infinite loop of creating new classes. "Once class A has been created, create class A" is what your code is saying.
     
  15. I typed exactly what @BreezerFly told me to type so... idk... let me check the logs

    Code:
    [Server thread/ERROR]: Could not load 'plugins\OreRunner.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:291) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:740) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-f27339c]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at me.unc00ked.LeaderboardFunctions.<init>(LeaderboardFunctions.java:6) ~[?:?]
        at me.unc00ked.OreRunner.<init>(OreRunner.java:42) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_66]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_66]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_66]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_66]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_66]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        ... 21 more
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at me.unc00ked.OreRunner.<init>(OreRunner.java:37) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_66]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_66]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_66]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_66]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_66]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-f928e7a-f27339c]
        ... 21 more
    line 42 is:
    Code:
    private LeaderboardFunctions lbFunc = new LeaderboardFunctions();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 26, 2015
  16. Offline

    teej107

    @UNC00KED You can't instantiate a JavaPlugin class on your own. Don't make your Leaderboards class extend JavaPlugin
     
  17. Post source
    And yeah, as @teej107 said. Nor do should it.
     
  18. Main Class:
    Code:
    public class OreRunner extends JavaPlugin {
      
        Logger logger = Logger.getLogger("Minecraft");
      
        public static OreRunner plugin;
        private LeaderboardFunctions lbFunc = new LeaderboardFunctions();
    
        @Override
        public void onEnable() {
            logger.info("OreRunner Enabled!");
            new LeaderboardFunctions();
            plugin = this;
        }
      
        @Override
        public void onDisable() {
            logger.info("OreRunner Disabled!");
            plugin = null;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("test") && sender instanceof Player) {
              
                Player player = (Player) sender;
              
                lbFunc.test(player);
              
                return true;
              
            }
          
            return false;
    
        }
    
    }

    LeaderboardFunctions Class:
    Code:
    public class LeaderboardFunctions extends JavaPlugin {
      
        public static LeaderboardFunctions plugin;
      
        void test(Player player) {
          
            player.sendMessage("Your name is: " + player.getName());
          
        }
      
    }
    How would you recommend then doing it? There's gotta be a way to make an instance of another class inside your main class... How do you do it @teej107 in your plugins?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  19. Offline

    teej107

    @UNC00KED You can only have one class extending JavaPlugin. No more or no less. Only one. Not three. Not four. Five is right out! Can only be one.
     
  20. Offline

    mug561

    Sidenote: Stop watching TheBCBroz.
    Use google. It's really helpful.

    And as far as your problem: ever heard of Constructors?

    they're really useful and one of the main functions of java (and object oriented programming in general)

    Google it.
     
    Zombie_Striker likes this.
  21. Thanks for the help guys @mug561 @teej107 @BreezerFly. Threw in a constructor and removed the "extends JavaPlugin" from my other class and now it's working perfectly.
     
Thread Status:
Not open for further replies.

Share This Page