Solved Why is this giving me a NullPointerException

Discussion in 'Plugin Development' started by Zach_1919, Mar 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Zach_1919

    Let me just start off by saying that I do like the Bukkit configuration system, but only for simple things, like setting values of different properties and whatnot. However, I cannot stand it for complicated things like large series of lists and all that. I am making a kits plugin for my server, and I want to use a new kind of configuration system where you create folders to house different files that are read and parsed as items. I set this up just fine, but this line of code is causing some troubles...
    Code:java
    1. for(File f : new File(Main.dir + "Kits/" + name + "/Contents").listFiles()) contents.add(loadItemStack(f,false));

    Now don't think I am stupid...loadKit is in fact a method in this class, but the error is not occurring in there because console would tell me what line of that method the error occurred in if that was the case. So it is something on this line that needs adjusting. Here is the rest of the code for my Kit class:
    Code:java
    1. package me.zDev.quantikits;
    2.  
    3. import java.io.File;
    4. import java.nio.charset.Charset;
    5. import java.nio.file.Files;
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.List;
    9.  
    10. import org.bukkit.Material;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.PlayerInventory;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16.  
    17. public class Kit
    18. {
    19. public String name;
    20. public List<ItemStack> contents;
    21. public ItemStack helmet, chestplate, leggings, boots;
    22.  
    23. public Kit(String key)
    24. {
    25. name = key;
    26. contents = new ArrayList<ItemStack>();
    27. try
    28. {
    29. for(File f : new File(Main.dir + "Kits/" + name + "/Contents").listFiles()) contents.add(loadItemStack(f,false));
    30. helmet = loadItemStack(new File(Main.dir + "Kits/" + name + "/helmet"),true);
    31. chestplate = loadItemStack(new File(Main.dir + "Kits/" + name + "/chestplate"),true);
    32. leggings = loadItemStack(new File(Main.dir + "Kits/" + name + "/leggings"),true);
    33. boots = loadItemStack(new File(Main.dir + "Kits/" + name + "/boots"),true);
    34.  
    35. } catch(Exception e) {
    36. helmet = new ItemStack(Material.DIAMOND_HELMET);
    37. chestplate = new ItemStack(Material.AIR);
    38. leggings = new ItemStack(Material.AIR);
    39. boots = new ItemStack(Material.AIR);
    40. e.printStackTrace();
    41. }
    42. }
    43.  
    44. private ItemStack setName(ItemStack i, String s)
    45. {
    46. ItemMeta im = i.getItemMeta();
    47. im.setDisplayName(s);
    48. i.setItemMeta(im);
    49. return i;
    50. }
    51.  
    52. private ItemStack loadItemStack(File f, boolean armor) throws Exception
    53. {
    54. Material material = armor ? Material.AIR : Material.valueOf(f.getName().toUpperCase());
    55. HashMap<Enchantment,Integer> enchantments = new HashMap<Enchantment,Integer>();
    56. String itemName = "";
    57. short dataValue = 0;
    58. int amount = 1;
    59. List<String> strings = Files.readAllLines(f.toPath(),Charset.forName("UTF-8"));
    60. if(armor)
    61. {
    62. material = Material.valueOf(strings.get(0).toUpperCase());
    63. strings.remove(0);
    64. }
    65. itemName = strings.get(0); strings.remove(0);
    66. amount = Integer.parseInt(strings.get(0)); strings.remove(0);
    67. dataValue = Short.parseShort(strings.get(0)); strings.remove(0);
    68. if(!strings.isEmpty())
    69. {
    70. for(String s : strings)
    71. {
    72. String[] array = s.split(":");
    73. enchantments.put(Enchantment.getByName(array[0].toUpperCase()),Integer.parseInt(array[1]));
    74. }
    75. }
    76. ItemStack itemstack = setName(new ItemStack(material,amount,dataValue),itemName);
    77. itemstack.addUnsafeEnchantments(enchantments);
    78. return itemstack;
    79. }
    80.  
    81. public void loadKit(Player p)
    82. {
    83. PlayerInventory inv = p.getInventory();
    84. inv.setHelmet(helmet);
    85. inv.setChestplate(chestplate);
    86. inv.setLeggings(leggings);
    87. inv.setBoots(boots);
    88. for(ItemStack i : contents) inv.addItem(i);
    89. Main.playerKits.put(p.getName(),this);
    90. }
    91. }


    And this is where I create this instance:
    Code:java
    1. package me.zDev.quantikits;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Main extends JavaPlugin implements Listener
    11. {
    12. public static HashMap<String,Kit> playerKits;
    13. public static String dir;
    14.  
    15. public void onEnable()
    16. {
    17. getServer().getPluginManager().registerEvents(this,this);
    18. playerKits = new HashMap<String,Kit>();
    19. dir = getDataFolder().getAbsolutePath();
    20. }
    21.  
    22. @EventHandler
    23. public void onInteract(PlayerInteractEvent event)
    24. {
    25. new Kit("PvP").loadKit(event.getPlayer());
    26. event.getPlayer().sendMessage(dir);
    27. }
    28. }
     
  2. Offline

    Barinade

    Maybe no such directory/no files?
     
  3. Offline

    Zach_1919

  4. Offline

    Barinade

    do null checks on every variable you have used
     
  5. Offline

    Zach_1919

    WOO HOO Barinade It turns out that I just needed a slash inside of one of my paths :/

    SCREW YOU BUKKIT CONFIGURATION SYSTEM
     
  6. Offline

    Barinade

    The File class is not Bukkit's lol
     
Thread Status:
Not open for further replies.

Share This Page