null while Material.valueOf

Discussion in 'Spigot Plugin Development' started by HardStyl3r, May 29, 2016.

Thread Status:
Not open for further replies.
  1. Hi, I've got a problem with my plugin.
    Here's a code:
    Code (Text):

        public static ItemStack getBlazeFishingRodItemStack(){
            String it = Main.getConfiguration().getBlazeFishingRodItem();
            Material item = Material.matchMaterial(it);
            String name = Main.getConfiguration().getBlazeFishingRodName();
            List<String> lore = Main.getConfiguration().getBlazeFishingRodLore();
            ItemStack is = Utils.createItem(item, 1, name, lore);
            return is;
        }
     
    problem is with getters: they are getting string while I am sending it (sendMessage) but there are nulls.

    stacktrace:
    Code (Text):

    [14:42:11] [Server thread/ERROR]: Error occurred while enabling hs_Tools v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.hardstyl3r.tools.Main.getBlazeFishingRodItemStack(Main.java:206) ~[?:?]
        at me.hardstyl3r.tools.Main.onEnable(Main.java:44) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot.jar:git-Spigot-8a048fe-106ced0]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.loadPlugin(CraftServer.java:362) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.enablePlugins(CraftServer.java:322) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at net.minecraft.server.v1_9_R2.MinecraftServer.t(MinecraftServer.java:416) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at net.minecraft.server.v1_9_R2.MinecraftServer.l(MinecraftServer.java:381) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at net.minecraft.server.v1_9_R2.MinecraftServer.a(MinecraftServer.java:336) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at net.minecraft.server.v1_9_R2.DedicatedServer.init(DedicatedServer.java:268) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:532) [spigot.jar:git-Spigot-8a048fe-106ced0]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
     
     
  2. im guessing this is null
    Main.getConfiguration().getBlazeFishingRodItem();
     
  3. I said, all getters are working. Only here it's not.
     
  4. check if the string is null ?
     
  5. Where? Should I check if string is null when it works?
    [​IMG]
     
  6. Obviously there's something wrong. You fucked up somewhere.

    Try isolating where the issue is.

    Print getConfiguration != null
    Print getBlazeFishingRodCrafting != null
     
  7. getConfiguration and getBlazeFishingRodCrafting are working properly.
     
  8. Code (Java):
    java.lang.NullPointerException
        at me.hardstyl3r.tools.Main.getBlazeFishingRodItemStack(Main.java:206) ~[?:?]
        at me.hardstyl3r.tools.Main.onEnable(Main.java:44) ~[?:?]
    check the error again.
     
  9. I don't understand you.
     
  10. can you show us your "getBlazeFishingRodItemStack" method?
     
  11. Code (Text):
        public static ItemStack getBlazeFishingRodItemStack(){
            String it = Main.getConfiguration().getBlazeFishingRodItem();
            Material item = Material.matchMaterial(it);
            String name = Main.getConfiguration().getBlazeFishingRodName();
            List<String> lore = Main.getConfiguration().getBlazeFishingRodLore();
            ItemStack is = Utils.createItem(item, 1, name, lore);
            return is;
        }
    and the Utils.createItem:
    Code (Text):
        public static ItemStack createItem(Material dc, int typeID, String name, List<String> description){
            ItemStack i = new ItemStack(dc, 1, (byte)typeID);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
            ArrayList<String> lore = new ArrayList<>();
            for (String line : description) {
                lore.add(ChatColor.translateAlternateColorCodes('&', line));
            }
            im.setLore(lore);
            i.setItemMeta(im);
            return i;
        }
     
  12. Ok, what is line 206 of the "Main" class?
    Also make sure your configs are registered before using the methods, as you use this method in onEnable.
     
  13. have you prepare the File and FileConfiguration of your custom yml in onEnable() ? and how do you access Main class to the other one ?
     
  14. yes, configs are working, but only there they aren't.

    it's 206 line in my Main.class.
    Code (Text):
    Material item = Material.matchMaterial(it);
     
  15. Can you show the getBlazeFishingRodItem method?
    Also what does it return to you?
     
  16. Code (Text):

        public String getBlazeFishingRodItem(){
            return blazefishingroditem;
        }
     
    and
    Code (Text):

    this.blazefishingroditem = this.file.getString("blazefishingrod.item");
     
    file = config
    it just returns "FISHING_ROD"
     
  17. well I'm confused is the .file is really config ? just look at this 2
    Code (Java):
    File file; // did you used this one ?
    FileConfiguration file; // or this one?
     
  18. in Configuration.class:
    Code (Text):

        public Configuration(FileConfiguration file) {
            this.file = file; (it's private FileConfiguration file;)
            this.loadConfiguration();
        }
    and it's loaded by:
    Code (Text):

        public void loadConfiguration() {
            this.saveDefaultConfig();
            configuration = new Configuration(this.getConfig());
        }
     
  19. You can get Material with ...
    Code (Text):
    Material.getMaterial(it);
     
  20. use the FileConfiguration when getting string on a file something like this

    Code (Java):
    FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    // then get the string with it
    config.getString(path);
     
Thread Status:
Not open for further replies.