NullPointerException for items config

Discussion in 'Spigot Plugin Development' started by Blastzit, Jun 18, 2016.

  1. So right now I'm making a RPG plugin and I made it so that every item has its own config.
    However I keep receiving an error(NullPointerException) when loading the config. (Loading the recipes need to load the config)

    Error:
    Code (Text):

    java.lang.NullPointerException
            at me.blastzit.fantasizes.Fantasizes.loadRecipe(Fantasizes.java:155) ~[?:?]
            at me.blastzit.fantasizes.Fantasizes.onEnable(Fantasizes.java:27) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
     
    And my plugin code:
    Code (Java):

    public void loadRecipe() {
            File dir = new File(this.getDataFolder() + File.separator + "Items" + File.separator);
            File[] directoryListing = dir.listFiles();
           
            if (directoryListing != null) {
                for (File child : directoryListing) {
                    String cs = child.getName();
                    YamlConfiguration iconfig = YamlConfiguration.loadConfiguration(child);
                    Integer i;
                    if (iconfig.getString(String.valueOf(cs) + ".Recipe.Type").equals("Shaped")) {
                        ShapedRecipe recipe = new ShapedRecipe(this.getConfig().getItemStack(String.valueOf(cs) + ".ItemName"));
                        recipe.shape(new String[]{"123", "456", "789"});
                        i = 1;
                        while (i <= 9) {
                            try {
                                recipe.setIngredient(i.toString().toCharArray()[0], this.getConfig().getItemStack(String.valueOf(cs) + ".Recipe.Slot" + i + ".type").getData());
                            }
                            catch (NullPointerException var6_7) {
                                // empty catch block
                            }
                            i = i + 1;
                        }
                        Bukkit.getServer().addRecipe((Recipe)recipe);
                        continue;
                    }
                    if (iconfig.getString(String.valueOf(cs) + ".Recipe.Type").equals("Shapeless")) {
                        ShapelessRecipe recipe = new ShapelessRecipe(this.getConfig().getItemStack(String.valueOf(cs) + ".ItemName"));
                        i = 1;
                        while (i <= 9) {
                            try {
                                recipe.addIngredient(this.getConfig().getItemStack(String.valueOf(cs) + ".Recipe.Slot" + i + ".type").getData());
                            }
                            catch (NullPointerException var6_8) {
                                // empty catch block
                            }
                            i = i + 1;
                        }
                        Bukkit.getServer().addRecipe((Recipe)recipe);
                        continue;
                    }
                    if (!iconfig.getString(String.valueOf(cs) + ".Recipe.Type").equals("Furnace")) continue;
                    FurnaceRecipe recipe = new FurnaceRecipe(this.getConfig().getItemStack(String.valueOf(cs) + ".ItemName"), this.getConfig().getItemStack(String.valueOf(cs) + ".Input").getData());
                    Bukkit.getServer().addRecipe((Recipe)recipe);
                }
            }
        }
     
    I want to load every recipe in a folder which is called "Items" and its inside the data folder of the plugin. I'll also post one of the items config here:
    Code (Text):

    Test:
      Name: Test
      Level: 1
      Rarity: 1
      Description: INITIALIZE
      DescriptionNum: 0
      Armor: 0
      ItemName: STONE
      health: 1
      Magic: 1
      Speed: 1
      Genre: INITIALIZE
      Race: INITIALIZE
      Recipe:
      Slot5:
      ==: org.bukkit.inventory.ItemStack
       type: STONE
      Type: Shaped
     
    Please help! :(
     
  2. What is loadRecipes line 155 and onEnable line 27?
     
  3. loadRecipes line 155 is this:
    Code (Java):
    if(iconfig.getString(String.valueOf(cs)+".Recipe.Type").equals("Shaped")){
     
  4. Code (Text):
    if(iconfig.getString(cs + ".Recipe.Type", "").equals("Shaped")){
    Added a default value of empty String so it doesnt return null if key not found. Also removed the valueOf because cs is already a String.
     
    • Useful Useful x 1
  5. Thanks, it doesn't give out an error but, the recipe is useless now. Even if I added a recipe, I can't use a crafting table to craft it. Any idea why?
     
  6. Add some debug messages to you code to see whats actually firing or not. It was previously returning null, thats why you got an NPE (null.equals("Shaped")). I fixed the NPE by not letting it be null ("".equals("Shaped")). The condition still isnt being met.
     
    • Useful Useful x 1
  7. use try catch when you were adding shape shapeless recipe and more
     
  8. I understand why it isn't working but is there a way to make it works then? Why can't it get the string though?