File NullpointerException

Discussion in 'Plugin Development' started by Irantwomiles, Aug 8, 2016.

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

    Irantwomiles

    Code:
    File file = null;  
    
    public void loadKit(Player player) {
          
            try {
            file = new File(plugin.getDataFolder(), player.getUniqueId().toString() + ".yml"); //Seems to be the problem here
            } catch (Exception e) {
                e.printStackTrace();
            }
          
            if(file.exists()) { //Problem also here
                new YamlConfiguration();
                YamlConfiguration inv = YamlConfiguration.loadConfiguration(file);
              
                if(!inv.contains("Inventory")) {
                    player.sendMessage(ChatColor.RED + "Could not find a kit!");
                    return;
                }
              
                ItemStack[] contents = player.getInventory().getContents();
                ItemStack[] armorContents = player.getInventory().getArmorContents();
              
                List<?> items = inv.getList("Inventory.items");
                List<?> armor = inv.getList("Inventory.armor");
              
                for(int i = 0; i < items.size(); i++) {
                    contents[i] = (ItemStack) items.get(i);
                }
              
                for(int i = 0; i < armor.size(); i++) {
                    armorContents[i] = (ItemStack) armor.get(i);
                }
              
                player.getInventory().clear();
                player.getInventory().setContents(contents);
                player.getInventory().setArmorContents(armorContents);
                player.updateInventory();
                player.sendMessage("Loaded your Kit!");
            } else {
              
                file = new File(plugin.getDataFolder(), "default.yml");
                YamlConfiguration inv = YamlConfiguration.loadConfiguration(file);
              
                ItemStack[] contents = player.getInventory().getContents();
                ItemStack[] armorContents = player.getInventory().getArmorContents();
              
                List<?> items = inv.getList("Inventory.items");
                List<?> armor = inv.getList("Inventory.armor");
              
                for(int i = 0; i < items.size(); i++) {
                    contents[i] = (ItemStack) items.get(i);
                }
              
                for(int i = 0; i < armor.size(); i++) {
                    armorContents[i] = (ItemStack) armor.get(i);
                }
              
                player.getInventory().clear();
                player.getInventory().setContents(contents);
                player.getInventory().setArmorContents(armorContents);
                player.updateInventory();
              
                player.sendMessage(ChatColor.RED + "Could not find a kit, loading default kit now!");
            }
        }
    
    Not really sure what could be null. I have the file created in my dataFolder just to test out but I still get nullpointer. I'm pretty much using the exact code to save/load kits so I don't know whats wrong
     
  2. Offline

    Zombie_Striker

    @Irantwomiles
    Is plugin null? If you try to get the DataFolder by itself, does it return a null file?
     
  3. Offline

    Irantwomiles

    Hmm didn't try checking if datafolder is null. Give me a second.

    EDIT: All of my problems lead up to when I try to initialize the file in my method.
    Code:
     file = new File(plugin.getDataFolder(), player.getUniqueId().toString(); //This is the probelm, keeps giving me NPE
    EDIT: Ok so I checked whether the plugin is null or not using this
    Code:
     
    
    Practice plugin; //Practice is my main class
    
    if(plugin == null) { sysout("plugin is null"); } //and it returns that in console, how is my plugin null? All of the other classes work just fine.
     
    Last edited: Aug 8, 2016
  4. @Irantwomiles
    Show us where you set the value to the "plugin" variable.
     
  5. Offline

    Irantwomiles

    I did, look one message up. That is in my global space of the class containing the method.
     
  6. @Irantwomiles
    Well, you never define the variable there, only initialize it, meaning it'll be null. You have to set it to your instance of the main class somehow.
     
Thread Status:
Not open for further replies.

Share This Page