Solved Config file and mainclass accessibility problem

Discussion in 'Spigot Plugin Development' started by ArKeid0s, Jan 20, 2020.

  1. Hey all I have a huge problem.
    My MenuClass can't access to the instabnce of main
    Here is my MainClass :
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin {

        Commands commands = new Commands(this);
        PetManager pM = new PetManager();
     
        private static MainClass instance;
     
        private void setInstance(MainClass instance) {
            instance = MainClass.instance;
        }

        @Override
        public void onEnable() {
            System.out.println("ArkPets is ON");
         
            setInstance(this);
         
            //Config
            this.getConfig().options().copyDefaults(true);
            saveDefaultConfig();

            //Commands
            getCommand(Commands.cmd1).setExecutor(commands);

            //Events
            getServer().getPluginManager().registerEvents(new EventsClass(this), this);
            getServer().getPluginManager().registerEvents(new MenuHandler(this), this);
         
        }

        @Override
        public void onDisable() {
            System.out.println("ArkWarps is OFF");
        }
    }
     
    Here is my Commands class

    Code (Java):
    package fr.arkeidos.arkpets;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    public class Commands implements CommandExecutor, Listener {

        private MainClass main;
        private FileConfiguration config;

        public Commands(MainClass mainclass) {
            main = mainclass;
            config = main.getConfig();
        }
     
        //Commands
        public static String cmd1 = "pet";

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
             
                MenuClass mainMenu = new MenuClass(main);

                if(cmd.getName().equalsIgnoreCase(cmd1)) {
                    mainMenu.createMainMenu(player);
                    player.sendMessage(config.getString("mainmenu"));
                }

            } else {
                sender.sendMessage("Only players can use this command !");
            }
            return false;
        }

    }
    Here is my MenuClass class

    Code (Java):
    package fr.arkeidos.arkpets;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;

    public class MenuClass implements Listener {

        private MainClass main;
        private static FileConfiguration config;
     
        public MenuClass(MainClass mainclass) {
            main = mainclass;
            config = main.getConfig();
        }

        ////VARIABLES
        //Inventories Names
        public static String invMainMenuName = config.getString("mainmenu");
        public static String invCreaMenuName = "creation-menu";
        public static String invCreaArmorMenu = "armor-menu";

        //Menus Items Names
        // - Main menu
        public static String petRemoveItem = "remove-pet-button";
        public static String petCloseItem = "close-button";
        public static String petItemCrea = "create-pet-button";

        // - Creation Menu
        public static String petItemCreaVisible = "visible-button";
        public static String petItemCreaSmall = "small-button";
        public static String petItemCreaGlowing = "glowing-button";
        public static String petItemCreaArms = "arms-button";
        public static String petItemCreaBase = "base-button";
        public static String petItemCreaChestplate = "chestplate-button";
        public static String petItemCreaHead = "head-button";
        public static String petItemCreaItemInHand = "itemInHand-button";
        public static String petItemCreaValidation = "validation-button";
        public static String petItemCreaCancel = "cancel-button";

        // - Armor Menu
        public static String petItemArmorLeather = "leather-button";
        public static String petItemArmorChainmail = "chainmail-button";
        public static String petItemArmorIron = "iron-button";
        public static String petItemArmorGold = "golden-button";
        public static String petItemArmorDiamond = "diamond-button";
        public static String petItemArmorNone = "none-button";

        //Main menu : /pet
        public void createMainMenu(Player player) {

            Inventory invMainMenu = Bukkit.createInventory(null, 9, invMainMenuName);

            //Creation Button
            ItemStack itCrea = new ItemStack(Material.ARMOR_STAND);
            ItemMeta itmCrea = itCrea.getItemMeta();
            ArrayList<String> itCreaLore = new ArrayList<String>();
            itCreaLore.add("create-pet-button-lore");
            itmCrea.setDisplayName(petItemCrea);
            itmCrea.setLore(itCreaLore);
            itCrea.setItemMeta(itmCrea);

            //Remove button
            ItemStack itR = new ItemStack(Material.BARRIER);
            ItemMeta itmR = itR.getItemMeta();
            ArrayList<String> itLoreR = new ArrayList<String>();
            itLoreR.add("remove-pet-button-lore");
            itmR.setDisplayName(petRemoveItem);
            itmR.addEnchant(Enchantment.DAMAGE_ALL, 1, false);
            itmR.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            itmR.setLore(itLoreR);
            itR.setItemMeta(itmR);

            //Close button
            ItemStack itC = new ItemStack(Material.BARRIER);
            ItemMeta itmC = itC.getItemMeta();
            ArrayList<String> itLoreC = new ArrayList<String>();
            itLoreC.add("close-button-lore");
            itmC.setDisplayName(petCloseItem);
            itmC.setLore(itLoreC);
            itC.setItemMeta(itmC);
         
            //Fill Item
            ItemStack itFill = new ItemStack(Material.BLACK_CONCRETE);
            ItemMeta itmFill = itFill.getItemMeta();
            itmFill.setDisplayName(" ");
            itFill.setItemMeta(itmFill);

            fillEmptySlots(invMainMenu, itFill);
         
            invMainMenu.setItem(3, itCrea);
            invMainMenu.setItem(5, itR);
            invMainMenu.setItem(8, itC);

            player.openInventory(invMainMenu);
        }

        @SuppressWarnings("deprecation")
        public void createPetCreationMenu(Player player) {

            Inventory invPetCreation = Bukkit.createInventory(null, 27, invCreaMenuName);

            //Visible or not
            ItemStack itVisible = new ItemStack(Material.GLASS);
            ItemMeta itmVisible = itVisible.getItemMeta();
            ArrayList<String> itVisibleLore = new ArrayList<String>();
            itVisibleLore.add("visible-button-lore1");
            itVisibleLore.add("visible-button-lore2");
            itmVisible.setDisplayName(petItemCreaVisible);
            itmVisible.setLore(itVisibleLore);
            itVisible.setItemMeta(itmVisible);

            //Glowing or not
            ItemStack itGlowing = new ItemStack(Material.SPECTRAL_ARROW);
            ItemMeta itmGlowing = itGlowing.getItemMeta();
            ArrayList<String> itGlowingLore = new ArrayList<String>();
            itGlowingLore.add("glowing-button-lore1");
            itGlowingLore.add("glowing-button-lore2");
            itmGlowing.setDisplayName(petItemCreaGlowing);
            itmGlowing.setLore(itGlowingLore);
            itGlowing.setItemMeta(itmGlowing);

            //Small or not
            ItemStack itSmall = new ItemStack(Material.TOTEM_OF_UNDYING);
            ItemMeta itmSmall = itSmall.getItemMeta();
            ArrayList<String> itSmallLore = new ArrayList<String>();
            itSmallLore.add("small-button-lore1");
            itSmallLore.add("small-button-lore2");
            itmSmall.setDisplayName(petItemCreaSmall);
            itmSmall.setLore(itSmallLore);
            itSmall.setItemMeta(itmSmall);

            //Head
            ItemStack itHead = new ItemStack(Material.PLAYER_HEAD);
            SkullMeta itmHeadSk = (SkullMeta) itHead.getItemMeta();
            ArrayList<String> itHeadLore = new ArrayList<String>();
            itHeadLore.add("head-button-lore1");
            itHeadLore.add("head-button-lore2");
            itmHeadSk.setOwningPlayer(Bukkit.getOfflinePlayer(player.getName()));
            itmHeadSk.setDisplayName(petItemCreaHead);
            itmHeadSk.setLore(itHeadLore);
            itHead.setItemMeta(itmHeadSk);

            //Chestplate
            ItemStack itChestplate = new ItemStack(Material.IRON_CHESTPLATE);
            ItemMeta itmChestplate = itChestplate.getItemMeta();
            ArrayList<String> itChestplateLore = new ArrayList<String>();
            itChestplateLore.add("chestplate-button-lore");
            itmChestplate.setDisplayName(petItemCreaChestplate);
            itmChestplate.setLore(itChestplateLore);
            itChestplate.setItemMeta(itmChestplate);

            //BasePlate or not
            ItemStack itBase = new ItemStack(Material.SMOOTH_STONE_SLAB);
            ItemMeta itmBase = itBase.getItemMeta();
            ArrayList<String> itBaseLore = new ArrayList<String>();
            itBaseLore.add("base-button-lore1");
            itBaseLore.add("base-button-lore2");
            itmBase.setDisplayName(petItemCreaBase);
            itmBase.setLore(itBaseLore);
            itBase.setItemMeta(itmBase);

            //Item In Hand
            ItemStack itItemInHand = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta itmItemInHand = itItemInHand.getItemMeta();
            ArrayList<String> itItemInHandLore = new ArrayList<String>();
            itItemInHandLore.add("itemInHand-button-lore1");
            itItemInHandLore.add("itemInhand-button-lore2");
            itmItemInHand.setDisplayName(petItemCreaItemInHand);
            itmItemInHand.setLore(itItemInHandLore);
            itItemInHand.setItemMeta(itmItemInHand);

            //Arms
            ItemStack itArms = new ItemStack(Material.STICK);
            ItemMeta itmArms = itArms.getItemMeta();
            ArrayList<String> itArmsLore = new ArrayList<String>();
            itArmsLore.add("arms-button-lore1");
            itArmsLore.add("close-button-lore2");
            itmArms.setDisplayName(petItemCreaArms);
            itmArms.setLore(itArmsLore);
            itArms.setItemMeta(itmArms);

            //Validate
            ItemStack itValidation = new ItemStack(Material.GREEN_WOOL);
            ItemMeta itmValidation = itValidation.getItemMeta();
            ArrayList<String> itValidationLore = new ArrayList<String>();
            itValidationLore.add("validation-button-lore");
            itmValidation.setDisplayName(petItemCreaValidation);
            itmValidation.setLore(itValidationLore);
            itValidation.setItemMeta(itmValidation);

            //Cancel
            ItemStack itCancel = new ItemStack(Material.RED_WOOL);
            ItemMeta itmCancel = itCancel.getItemMeta();
            ArrayList<String> itCancelLore = new ArrayList<String>();
            itCancelLore.add("cancel-button-lore");
            itmCancel.setDisplayName(petItemCreaCancel);
            itmCancel.setLore(itCancelLore);
            itCancel.setItemMeta(itmCancel);
         
            //Fill Item
            ItemStack itFill = new ItemStack(Material.BLACK_CONCRETE);
            ItemMeta itmFill = itFill.getItemMeta();
            itmFill.setDisplayName(" ");
            itFill.setItemMeta(itmFill);
         
            fillEmptySlots(invPetCreation, itFill);

            invPetCreation.setItem(1, itVisible);
            invPetCreation.setItem(10, itGlowing);
            invPetCreation.setItem(19, itSmall);
            invPetCreation.setItem(4, itHead);
            invPetCreation.setItem(13, itChestplate);
            invPetCreation.setItem(22, itBase);
            invPetCreation.setItem(12, itItemInHand);
            invPetCreation.setItem(14, itArms);
            invPetCreation.setItem(8, itValidation);
            invPetCreation.setItem(26, itCancel);

            player.openInventory(invPetCreation);
        }

        public void createArmorMenu(Player player) {

            Inventory invPetArmor = Bukkit.createInventory(null, 9, invCreaArmorMenu);

            //Leather Armor
            ItemStack itLeather = new ItemStack(Material.LEATHER_CHESTPLATE);
            ItemMeta itmLeather = itLeather.getItemMeta();
            itmLeather.setDisplayName(petItemArmorLeather);
            itLeather.setItemMeta(itmLeather);

            //Chainmail Armor
            ItemStack itChainmail = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
            ItemMeta itmChainmail = itChainmail.getItemMeta();
            itmChainmail.setDisplayName(petItemArmorChainmail);
            itChainmail.setItemMeta(itmChainmail);

            //Iron Armor
            ItemStack itIron = new ItemStack(Material.IRON_CHESTPLATE);
            ItemMeta itmIron = itIron.getItemMeta();
            itmIron.setDisplayName(petItemArmorIron);
            itIron.setItemMeta(itmIron);

            //Golden Armor
            ItemStack itGold = new ItemStack(Material.GOLDEN_CHESTPLATE);
            ItemMeta itmGold = itGold.getItemMeta();
            itmGold.setDisplayName(petItemArmorGold);
            itGold.setItemMeta(itmGold);

            //Diamond Armor
            ItemStack itDiamond = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta itmDiamond = itDiamond.getItemMeta();
            itmDiamond.setDisplayName(petItemArmorDiamond);
            itDiamond.setItemMeta(itmDiamond);

            //None
            ItemStack itNone = new ItemStack(Material.BARRIER);
            ItemMeta itmNone = itNone.getItemMeta();
            itmNone.setDisplayName(petItemArmorNone);
            itNone.setItemMeta(itmNone);
         
            //Fill Item
            ItemStack itFill = new ItemStack(Material.BLACK_CONCRETE);
            ItemMeta itmFill = itFill.getItemMeta();
            itmFill.setDisplayName(" ");
            itFill.setItemMeta(itmFill);

            fillEmptySlots(invPetArmor, itFill);

            invPetArmor.setItem(1, itLeather);
            invPetArmor.setItem(2, itChainmail);
            invPetArmor.setItem(3, itIron);
            invPetArmor.setItem(4, itGold);
            invPetArmor.setItem(5, itDiamond);
            invPetArmor.setItem(7, itNone);

            player.openInventory(invPetArmor);
        }
     
        public void fillEmptySlots(Inventory inv, ItemStack item) {
            for (int i = 0; i < inv.getSize(); i++) {
                if(inv.getItem(i) == null || inv.getItem(i).getType().equals(Material.AIR)) {
                    inv.setItem(i, item);
                }
            }
        }
    }
     
    And finally the error

    Code (Text):

    [13:26:48] [Server thread/INFO]: ArKeid0s issued server command: /pet
    [13:26:48] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pet' in plugin ArkPets v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:711) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1657) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1497) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:917) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:910) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:827) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
    Caused by: java.lang.ExceptionInInitializerError
        at fr.arkeidos.arkpets.Commands.onCommand(Commands.java:28) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        ... 17 more
    Caused by: java.lang.NullPointerException
        at fr.arkeidos.arkpets.MenuClass.<clinit>(MenuClass.java:29) ~[?:?]
        at fr.arkeidos.arkpets.Commands.onCommand(Commands.java:28) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        ... 17 more

     
    I hope that someone can help me with that
    Thanks
     
  2. In your onEnable() add:
    new MenuClass(this);
     
  3. Here is the result :
    Code (Java):
    @Override
        public void onEnable() {
            System.out.println("ArkPets is ON");
           
            setInstance(this);
           
            new MenuClass(this);
           
            //Config
            this.getConfig().options().copyDefaults(true);
            saveDefaultConfig();

            //Commands
            getCommand(Commands.cmd1).setExecutor(commands);

            //Events
            getServer().getPluginManager().registerEvents(new EventsClass(this), this);
            getServer().getPluginManager().registerEvents(new MenuHandler(this), this);
           
        }
    Here is the error on restart :

    Code (Text):
    20.01 13:59:08 [Server] Server thread/ERROR Error occurred while enabling ArkPets v0.1 (Is it up to date?)
    20.01 13:59:08 [Server] INFO java.lang.ExceptionInInitializerError: null
    20.01 13:59:08 [Server] INFO at fr.arkeidos.arkpets.MainClass.onEnable(MainClass.java:22) ~[?:?]
    20.01 13:59:08 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:462) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:376) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:456) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:266) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:783) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    20.01 13:59:08 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
    20.01 13:59:08 [Server] INFO Caused by: java.lang.NullPointerException
    20.01 13:59:08 [Server] INFO at fr.arkeidos.arkpets.MenuClass.<clinit>(MenuClass.java:29) ~[?:?]
    20.01 13:59:08 [Server] INFO ... 10 more
     
  4. You have to create an instance of MenuClass instance. The class itself does nothing. Its like a blueprint.
    You need to create an object out of your "blueprint" with new MainClass
     
  5. Like that : MenuClass mC = new MenuClass(this); ?
     
  6. This happens when you abuse static and dont know what it does.

    Until you know EXACTLY what static does -> Do NOT use static ANYWHERE. Just dont.
    Try to write your code without any static element
     
  7. How? I can't i need my gui names in the menu handler class :/
     
  8. Oh I will test something later I think I found a solution to not use static
    And can you explain me what is the problem with static ?
     
  9. There is no problem with static. It is simply a keyword just like a bunch of others in Java.

    The problem lies within how static is sometimes used: as a way to access data, rather than a way to specify to what the data is linked. In essence static allows you to bind data to a class, rather than an object. So when a field in a class is static, that field can be accessed directly from that class and does not require an instance. This means that you can do something like MyClass.staticField instead of having to create an instance and then access it, like so new MyClass().staticField. It is very easy to just mark everything static: then you never have to make any instance of a class.

    This way of designing something, however, often makes things more difficult down the line, especially for languages that are built on an OO foundation. You hide the OO part of the language and give everything a global state: everything can access everything from everywhere, without limitations and restrictions. Yet these limitations and restrictions are often a very good thing: they allow you to write code that is more safe to use, since you are unable to perform operations that you shouldn't be able to perform. Hence static is not always a good solution to a problem; very often a better approach is to rethink your design.

    I should note that static is not always just bad. There are plenty of ways to make proper use of static without falling into the issues I mentioned earlier. However, static usage should be with caution; you need to ask yourself whether the field is really tied to the class and not an instance of it, or whether you are just marking the field as static out of convenience.
     
    • Agree Agree x 2
    • Useful Useful x 1
  10. Hmm I have changed all of my static variables but here is an error
    My mainclass :
    Code (Java):
    package fr.arkeidos.arkpets;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin {

        Commands commands = new Commands(this);
        PetManager pM;
        MenuClass mC;
       
        //private static MainClass instance;
       
        /*private void setInstance(MainClass instance) {
            instance = MainClass.instance;
        }*/


        @Override
        public void onEnable() {
            System.out.println("ArkPets is ON");
           
            mC = new MenuClass(this);
            pM = new PetManager();
            //setInstance(this);
           
            //Config
            this.getConfig().options().copyDefaults(true);
            saveDefaultConfig();

            //Commands
            getCommand(Commands.cmd1).setExecutor(commands);

            //Events
            getServer().getPluginManager().registerEvents(new EventsClass(this), this);
            getServer().getPluginManager().registerEvents(new MenuHandler(this), this);
           
        }

        @Override
        public void onDisable() {
            System.out.println("ArkWarps is OFF");
        }
    }
     
    My MenuClass :
    Code (Java):
    package fr.arkeidos.arkpets;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;

    public class MenuClass implements Listener {

        private MainClass main;
        private FileConfiguration config;
       
        public MenuClass(MainClass mainclass) {
            main = mainclass;
            config = main.getConfig();
        }

        ////VARIABLES
        //Inventories Names
        public String invMainMenuName = ChatColor.translateAlternateColorCodes('&', config.getString("main-menu"));
        public String invCreaMenuName = ChatColor.translateAlternateColorCodes('&', config.getString("creation-menu"));
        public String invCreaArmorMenu = ChatColor.translateAlternateColorCodes('&', config.getString("armor-menu"));
     
    (Just the beginning)

    And an example of my config.yml
    Code (YAML):
    # ArkPets version 0.1

    ########### GUI ##########
    # - Main Menu
    main-menu
    : "&dPet Menu"
    create-pet-button
    : "&aCreate a Pet"
    create-pet-button-lore
    : "&7Click to create your pet"
    remove-pet-button
    : "&4Remove"
    remove-pet-button-lore
    : "&7Click to remove your pet"
    close-button
    : "&cClose"
    close-button-lore
    : "&7Click to close the menu"
     
    And the error :
    Code (Text):
    [17:08:38] [Server thread/INFO]: ArkPets is ON
    [17:08:38] [Server thread/ERROR]: Error occurred while enabling ArkPets v0.1 (Is it up to date?)
    java.lang.NullPointerException: null
        at fr.arkeidos.arkpets.MenuClass.<init>(MenuClass.java:30) ~[?:?]
        at fr.arkeidos.arkpets.MainClass.onEnable(MainClass.java:21) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:462) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:376) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:456) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:266) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:783) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
     
  11. Your config field in the MenuClass class is null when the inventory names are being initialized (invMainMenuName specifically).
     
  12. So how can i fix that ?
     
  13. You need to initialize it with a value before invMainMenuName gets initialized.
     
  14. But a FilieConfiguration variable can't have a value ? or i am wrong
     
  15. Every variable can have a value.
     
  16. I don't see where to initialize it and how :/
    Sorry can you help me
     
  17. Where you initialize it doesn't matter, it only matters when you initialize it. Initializing a variable can be done by doing:
    Code (Java):
    myVariable = aValue;
     
  18. You have a NullPointerException at line 29 at
    Code (Java):
    public static String invMainMenuName = config.getString("mainmenu");
    That basically means that either config is not defined, or there's not "mainmenu" string inside of the config.yml

    You can use FileConfiguration#isSet(String key) to check if a value is set for a specific key and set a default value if it's not.
     
  19. This is what stef told me but i don't know how to initialize a config file
     
  20. There are always lots of documents, threads, and videos roaming around on the internet with an answer to your question. Next time I'd recommend doing a little search.

    You already created and copied the default config inside your onEnable() method, which is good.
    Now when grabbing data from the config, you use FileConfiguration#get(String key) but sometimes, like in this case, the key doesn't exist and returns an error. Therefore, you need to check first if it's set, and I recommend checking if it's a string too.
    You can check if a key is set by using FileConfiguration#isSet(String key), and you can check if it's a string by using FileConfiguration#isString(String key).

    Which would look like this:
    Code (Java):
    if(config.isSet("mainmenu") && config.isString("mainmenu")) {}
    and if that returns true, then you can grab the value with FileConfiguration#get(String key)

    and final code should look like this:
    Code (Java):

    if(config.isSet("mainmenu") && config.isString("mainmenu") {
        invMainMenuName = config.get("mainmenu"); // grab value if it is set
    } else {
        invMainMenuName = "DEFAULT_VALUE_HERE";
        config.set("mainmenu", invMainMenuName); // set the default value
        main.saveConfig(); // save config file
    }
     
    or just to make it really short and lazy (and I might be wrong) you can use FileConfiguration#get(String key, Object value) which returns the value you provided if the key doesn't exist.
    Code (Java):
    public static String invMainMenuName = getConfig().get("mainmenu", "DEFAULT_VALUE_HERE");
     
    #20 Rayo, Jan 20, 2020
    Last edited: Jan 20, 2020