Solved new ItemStack() returns null???

Discussion in 'Plugin Help/Development/Requests' started by Awesomebanana2002, Jul 30, 2015.

Thread Status:
Not open for further replies.
  1. Hi there, I've beed trying to fix this for about 4 hours now, and I cant get it to work. My method:
    Code:
        @SuppressWarnings("deprecation")
        private void parseIcon() {
            String format = Kits.get().getString(this.name + ".icon");
            String name = Kits.get().getString(this.name + ".display-name");
         
            int id = 1;
            short damage = 0;
         
            if(format.contains(":")) {
                String[] parts = format.split(":");
                id = Integer.parseInt(parts[0]);
                damage = (short) Integer.parseInt(parts[1]);
            }else{
                id = Integer.parseInt(format);
            }
         
         
            icon = new ItemStack(id, 1, damage);
         
            if(icon == null)
                Bukkit.getLogger().warning("icon == NULL");
         
            ItemMeta im = icon.getItemMeta();
            im.setDisplayName(name);
            icon.setItemMeta(im);
        }
    When you don't give the icon a damage, (without :1), it runs fine, but when you do, the variable icon is null..
    Does someone know how to fix this. If so, please help me out :)
     
  2. Offline

    tommyhoogstra

    I think 0 might mean its broken, so try with 1, or no number at all

    - just a guess, havent done bukkit programming in a while now.
     
  3. @tommyhoogstra Hmm.. I dont think thats the problem. It only sets the damage to 0 when it can't find ":" in the format.

    EDIT: Tried it.. Still won't work
     
  4. @Awesomebanana2002 there is no way that a constructor returns null. this is not possible in java. the only way would be that an exception is thrown what would be shown in the console. Is there any? Btw why aren't you saving the enum.toString() instead of the id? this way you could get the material by enum.valueOf(String)
     
  5. @Shmobi I wont save it, so the users that are using the plugin can set their own kits, and I might try enum.valueOf();
    And yes, I get an error. It happens when I set the display name to the ItemMeta. I did a check if that string was null, and it wasn't. It only gives the error when the format contains ":". Otherwise it sets the display name fine...

    Heres my error:
    Code:
    [13:04:06 WARN]: java.lang.NullPointerException
    [13:04:06 WARN]:        at com.TheWGB.SG.kits.Kit.parseIcon(Kit.java:97)
    [13:04:06 WARN]:        at com.TheWGB.SG.kits.Kit.<init>(Kit.java:31)
    [13:04:06 WARN]:        at com.TheWGB.SG.kits.KitManager.loadKits(KitManager.jav
    a:41)
    [13:04:06 WARN]:        at com.TheWGB.SG.Main.onEnable(Main.java:108)
    [13:04:06 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:321)
    [13:04:06 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:335)
    [13:04:06 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:405)
    [13:04:06 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin
    (CraftServer.java:356)
    [13:04:06 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlug
    ins(CraftServer.java:316)
    [13:04:06 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(Cra
    ftServer.java:746)
    [13:04:06 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:534)
    [13:04:06 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand.java:25)
    [13:04:06 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:141)
    [13:04:06 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCo
    mmand(CraftServer.java:646)
    [13:04:06 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchSe
    rverCommand(CraftServer.java:632)
    [13:04:06 WARN]:        at net.minecraft.server.v1_8_R1.DedicatedServer.aM(Dedic
    atedServer.java:353)
    [13:04:06 WARN]:        at net.minecraft.server.v1_8_R1.DedicatedServer.z(Dedica
    tedServer.java:317)
    [13:04:06 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.y(Minecr
    aftServer.java:634)
    [13:04:06 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.run(Mine
    craftServer.java:537)
    [13:04:06 WARN]:        at java.lang.Thread.run(Unknown Source)
    [13:04:06 WARN]: [SurvivalGames] Could not load kit apple-master correctly! java
    .lang.NullPointerException
    EDIT: Just checked.. Nothing is null in the method!!! And it gives me a NullPointerException.. What the...
     
    Last edited: Jul 30, 2015
  6. Offline

    Tecno_Wizard

    @Awesomebanana2002, did you consider that Bukkit is responsible for the null field?
    If a type does not have a data field, giving it one results in a nonexistant itemstack, causing bukkit to throw a null pointer.
     
  7. Fixed... Apparantly you cant use a ':' in the config.. This is what I used before: 'icon: 322:1' and now im using 'icon: 322 1'.... I just want to rage right now.
     
  8. Offline

    Boomer

    you can use colins in the configs, just make sure they are quoted values
    ...icon is your pathway/key '322:1' is the value

    icon: '322:1' works
     
Thread Status:
Not open for further replies.

Share This Page