Error when getting Main class in other class

Discussion in 'Plugin Development' started by AwesomeFishh, May 21, 2018.

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

    AwesomeFishh

    Whenever I launch the server with the plugin installed, I get this error:

    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalStateException: Cannot get plugin for class me.AwesomeFishh.ShardRewards.Main from a static initializer'
    Which is caused by 2 lines of my code, Line 7 in ConfigManager and 14 in Main:

    Code:
    Caused by: java.lang.IllegalStateException: Cannot get plugin for class me.AwesomeFishh.ShardRewards.Main from a static initializer
            at org.bukkit.plugin.java.JavaPlugin.getPlugin(JavaPlugin.java:531) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at me.AwesomeFishh.ShardRewards.Configurations.ConfigManager.<init>(ConfigManager.java:7) ~[?:?]
            at me.AwesomeFishh.ShardRewards.Main.<init>(Main.java:14) ~[?:?]
    This is the code on those lines:

    Main:
    Code:
    public String prefix = ChatColor.GOLD + "[ShardRewards]";
    public ConfigManager configManager = new ConfigManager();
    public ChestConfig chestConfig = new ChestConfig();
    ConfigManager:
    Code:
    private Main plugin = Main.getPlugin(Main.class);
    ChestConfig chestClass = plugin.chestConfig;

    I have the line from ConfigManager in several other classes, and they do not give any errors.

    Can anyone help me out here?

    EDIT: used Code thingys, and here's the whole project: https://github.com/AwesomeFishh/ShardRewards
     
    Last edited: May 21, 2018
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    AwesomeFishh

    Just added the github link for the whole project in the edit.
     
  4. Offline

    timtower Administrator Administrator Moderator

    And why not just use constructors and instances?
     
  5. Offline

    AwesomeFishh

    I'm kinda new to java, and this is how a tutorial did it, so what exactly do you mean by that?
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    AwesomeFishh

  8. Offline

    timtower Administrator Administrator Moderator

    That totally depends on the rest of the code.
     
  9. Offline

    AwesomeFishh

    Well it gives it on:

    ConfigManager:
    Code:
    ChestConfig chestClass = plugin.chestConfig;
    And Main:
    Code:
    public ConfigManager configManager = new ConfigManager(this);
    Does that mean that 'this' (which is the plugin right?) causes the nullpointer, and in the configmanager the same story, because the plugin is null?

    How can I fix that?
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    AwesomeFishh

    @timtower
    Code:
    package me.AwesomeFishh.ShardRewards;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.AwesomeFishh.ShardRewards.Commands.RegisterChest;
    import me.AwesomeFishh.ShardRewards.Configurations.ChestConfig;
    import me.AwesomeFishh.ShardRewards.Configurations.ConfigManager;
    import me.AwesomeFishh.ShardRewards.Events.Events;
    
    public class Main extends JavaPlugin {
       
        public String prefix = ChatColor.GOLD + "[ShardRewards]";
        public ConfigManager configManager = new ConfigManager(this);
        public ChestConfig chestConfig = new ChestConfig(this);
       
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new Events(this), this);
            getCommand("registerchests").setExecutor(new RegisterChest(this));
            configManager.setupChestFile();
            loadConfig();
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ShardRewards] Enabled!");
        }
       
        public void onDisable() {
            saveConfig();
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ShardRewards] Disabled!");
        }
       
        public void loadConfig() {
            getConfig().addDefault("chests.1.item", "COOKED_BEEF");
            getConfig().addDefault("chests.1.amount", 1);
            getConfig().addDefault("chests.1.name", "&6Steak");
            getConfig().addDefault("chests.1.command.1", "/give %p minecraft:steak 1");
            getConfig().addDefault("chests.1.command.2", "/spawn %p");
            getConfig().addDefault("chests.1.shardscost", "1");
            getConfig().getDefaults().options().copyDefaults(true);
            saveConfig();
        }
    
    }
    
    Code:
    package me.AwesomeFishh.ShardRewards.Configurations;
    
    import me.AwesomeFishh.ShardRewards.Main;
    
    public class ConfigManager {
       
        Main plugin;
        ChestConfig chestClass = plugin.chestConfig;
       
        public ConfigManager(Main plugin) {
            this.plugin = plugin;
        }
       
        public void setupChestFile() {
            chestClass.createChests();
            chestClass.saveChestsCfg();
            chestClass.reloadChestsCfg();
        }
    
    }
    
     
  12. Offline

    timtower Administrator Administrator Moderator

    @AwesomeFishh That is probably because you are using plugin, before the constructor is being used.
     
  13. Offline

    AwesomeFishh

    @timtower Updated the code a bit, now put this (with some different class names ofc)
    Code:
    Main plugin;
        ChestConfig chestConfig;
        ConfigManager configManager;
    
        public RegisterChest(Main plugin) {
            this.plugin = plugin;
            configManager = plugin.configManager;
            chestConfig = plugin.chestConfig;
        }
    at the start of every class.

    I get a nullpointer in line22 on Main (which is the getCommand line):

    Code:
    package me.AwesomeFishh.ShardRewards;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.AwesomeFishh.ShardRewards.Commands.RegisterChest;
    import me.AwesomeFishh.ShardRewards.Configurations.ChestConfig;
    import me.AwesomeFishh.ShardRewards.Configurations.ConfigManager;
    import me.AwesomeFishh.ShardRewards.Events.Events;
    
    public class Main extends JavaPlugin {
    
        public String prefix = ChatColor.GOLD + "[ShardRewards]";
        public ConfigManager configManager;
        public ChestConfig chestConfig;
     
        public void onEnable() {
    
            configManager = new ConfigManager(this);
            chestConfig = new ChestConfig(this);
            this.getServer().getPluginManager().registerEvents(new Events(this), this);
            this.getCommand("registerchest").setExecutor(new RegisterChest(this));
            configManager.setupChestFile();
            loadConfig();
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ShardRewards] Enabled!");
        }
     
        public void onDisable() {
            saveConfig();
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ShardRewards] Disabled!");
        }
     
        public void loadConfig() {
            getConfig().addDefault("chests.1.item", "COOKED_BEEF");
            getConfig().addDefault("chests.1.amount", 1);
            getConfig().addDefault("chests.1.name", "&6Steak");
            getConfig().addDefault("chests.1.command.1", "/give %p minecraft:steak 1");
            getConfig().addDefault("chests.1.command.2", "/spawn %p");
            getConfig().addDefault("chests.1.shardscost", "1");
            getConfig().getDefaults().options().copyDefaults(true);
            saveConfig();
        }
    
    }
    
    RegisterChest.java:

    Code:
    package me.AwesomeFishh.ShardRewards.Commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.AwesomeFishh.ShardRewards.Main;
    import me.AwesomeFishh.ShardRewards.Configurations.ChestConfig;
    import me.AwesomeFishh.ShardRewards.Configurations.ConfigManager;
    
    public class RegisterChest implements CommandExecutor {
    
        Main plugin;
        ChestConfig chestConfig;
        ConfigManager configManager;
    
        public RegisterChest(Main plugin) {
            this.plugin = plugin;
            configManager = plugin.configManager;
            chestConfig = plugin.chestConfig;
        }
    
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("registerchest")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (args.length == 1) {
                        Block block = player.getTargetBlock(null, 200);
                        if (args[0].matches("^[0-9]*$")) {
                            int id = Integer.parseInt(args[0]);
                            if (!chestConfig.getChestsCfg().contains("chest." + id)) {
                                if (block.getType().getId() == 54) {
                                    chestConfig.getChestsCfg().set("chest." + id + ".world", player.getWorld());
                                    chestConfig.getChestsCfg().set("chest." + id + ".x", block.getLocation().getX());
                                    chestConfig.getChestsCfg().set("chest." + id + ".y", block.getLocation().getY());
                                    chestConfig.getChestsCfg().set("chest." + id + ".z", block.getLocation().getZ());
                                    player.sendMessage(plugin.prefix + ChatColor.YELLOW + " Chest with id " + id
                                            + "has been successfully registered!");
                                } else {
                                    player.sendMessage(plugin.prefix + ChatColor.YELLOW
                                            + " Please look at a chest while using this command!");
                                }
                            } else {
                                player.sendMessage(
                                        plugin.prefix + ChatColor.YELLOW + " A chest with that ID already exists!");
                            }
                        } else {
                            player.sendMessage(plugin.prefix + ChatColor.RED + " Please specificy a number for the ID!");
                        }
                    }
                }
            }
    
            return false;
        }
    
    }
    
     
  14. Offline

    timtower Administrator Administrator Moderator

    @AwesomeFishh Did you register that command in the plugin.yml?
     
  15. Offline

    AwesomeFishh

    @timtower Well shit.. I totally forgot that, didn't think that would cause a nullpointer though.

    I got another nullpointer but fixed it already, but I got a question about it:

    Code:
    package me.AwesomeFishh.ShardRewards.Configurations;
    
    import me.AwesomeFishh.ShardRewards.Main;
    
    public class ConfigManager {
    
        Main plugin;
        ChestConfig chestClass;
       
        public ConfigManager(Main plugin) {
            this.plugin = plugin;
        }
    
        public void setupChestFile() {
            chestClass = plugin.chestConfig;
            chestClass.createChests();
            chestClass.saveChestsCfg();
            chestClass.reloadChestsCfg();
        }
    
    }
    
    If I put the chestClass = plugin.chestConfig; either above, below or in the constructor, it gives a null error. Why does it not when I put it like this, in the setupChestFile method?
     
  16. Offline

    timtower Administrator Administrator Moderator

  17. Offline

    AwesomeFishh

    @timtower In the setupChestFile everything works fine, but when I put it in the constructor it gives nullPointerExceptions.
     
  18. Offline

    timtower Administrator Administrator Moderator

    @AwesomeFishh what is the nullpointerexception then? Because setting a value doesn't throw one.
    Code and error.
     
  19. Offline

    AwesomeFishh

    Well, in the code 3 posts above, when I put chestClass = plugin.chestConfig; in the constructor, chestClass.createChests(); throws a nullpointer, which I'm assuming is because chestClass == null, because createChests works fine when chestClass = plugin.chestConfig; is in the setupChestFile() method.
     
  20. Offline

    timtower Administrator Administrator Moderator

    Post the new code!
     
Thread Status:
Not open for further replies.

Share This Page