Solved Get material from .yml error

Discussion in 'Spigot Plugin Development' started by iNoXs, Jun 5, 2016.

  1. Hi !
    i try to get a material from my config but i have an error

    i tried with getMaterial(), and matchMaterial(), but return the same error :/

    My code :
    Code (Text):

        public ArrayList<ItemStack> getKitB() {
            ArrayList<ItemStack> i = new ArrayList<>();
            i.add(get(Material.matchMaterial(SettingsManager.getInstance().getData().getString("kit1.armor_helmet")), 1, (byte) 0, null, null, 1));
            i.add(get(Material.FLINT_AND_STEEL, 1, (byte) 0, null, null, 2));
            return i;
        }
    error :
    Code (Text):
    java.lang.ExceptionInInitializerError
        at fr.inoxs.simpleduel.kits.KitMenu.<init>(KitMenu.java:22) ~[?:?]
        at fr.inoxs.simpleduel.Main.onEnable(Main.java:53) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:333) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:412) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:352) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:313) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:742) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:643) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1105) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:940) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-"606148f"]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:684) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:625) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:528) [spigot.jar:git-Spigot-"606148f"]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.Material.matchMaterial(Material.java:658) ~[spigot.jar:git-Spigot-"606148f"]
        at fr.inoxs.simpleduel.kits.KitsContent.getKitB(KitsContent.java:23) ~[?:?]
        at fr.inoxs.simpleduel.kits.Kits.<clinit>(Kits.java:16) ~[?:?]
        ... 24 more
    Thanks :)
     
  2. Please post your stacktrace.
     
  3. Try valueOf instead of matchMaterial
     
  4. Use

    Code (Text):
    Material.valueOf("DIAMOND");
     
  5. Code (Text):
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
    But i want no name on the items how to do ?
     
  6. Then use "". Null points to nothing "" is an empty string. You want an empty string.
     
  7. Code (Text):
        public ArrayList<ItemStack> getKitB() {
            ArrayList<ItemStack> i = new ArrayList<>();
            i.add(get(Material.valueOf(SettingsManager.getInstance().getData().getString("kit1.armor_helmet")), 1, (byte) 0, "", null, 1));
            i.add(get(Material.FLINT_AND_STEEL, 1, (byte) 0, "", null, 2));
            return i;
        }
    Code (Text):
        private ItemStack get(Material mat, int amount, byte data, String displayName, Enchantment ench, int enchLevel) {
            ItemStack i = new ItemStack(mat, amount, data);
            ItemMeta iM = i.getItemMeta();
            iM.setDisplayName(displayName);

            if (ench != null) {
                iM.addEnchant(ench, enchLevel, true);
            }

            i.setItemMeta(iM);
            return i;
        }
    Error :
    Code (Text):
    java.lang.ExceptionInInitializerError
        at fr.inoxs.simpleduel.kits.KitMenu.<init>(KitMenu.java:22) ~[?:?]
        at fr.inoxs.simpleduel.Main.onEnable(Main.java:53) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:333) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:412) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:352) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:313) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:742) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [spigot.jar:git-Spigot-"606148f"]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:643) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1105) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:940) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-"606148f"]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:684) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:625) [spigot.jar:git-Spigot-"606148f"]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:528) [spigot.jar:git-Spigot-"606148f"]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException: Name is null
        at java.lang.Enum.valueOf(Unknown Source) ~[?:1.8.0_91]
        at org.bukkit.Material.valueOf(Material.java:65) ~[spigot.jar:git-Spigot-"606148f"]
        at fr.inoxs.simpleduel.kits.KitsContent.getKitB(KitsContent.java:23) ~[?:?]
        at fr.inoxs.simpleduel.kits.Kits.<clinit>(Kits.java:16) ~[?:?]
        ... 24 more

    I also try to remove the Name options but same error
     
    #7 iNoXs, Jun 5, 2016
    Last edited: Jun 5, 2016
  8. Alright so you can't do this.
    Code (Text):
    Material.valueOf(null);
    If you do it would throw that exception.

    Something in there is returning null. getData, getInstance or getString. You should debug by printing each one of those out to see which is null. Most likely the getString. Then you can narrow down the issue.
     
  9. Idk i maybe the string but
    Code (Text):
     
    SettingsManager.getInstance().getData().addDefault("kit1.armor.helmet", "DIAMOND_HELMET");
    and my data.yml now :
    Code (Text):
    kit1:
      gui_item_kit1: DIAMOND_SWORD
      gui_item_kit1_name: "\xa7cDefault kit 1"
      armor:
        helmet: DIAMOND_HELMET
        chestplate: DIAMOND_CHESTPLATE
        leggings: DIAMOND_LEGGINGS
        boots: DIAMOND_BOOTS
      sword: DIAMOND_SWORD
    SettingsManager.getInstance().getData().addDefault() is working