How is this null?!

Discussion in 'Plugin Development' started by XxZHALO13Xx, Dec 28, 2014.

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

    XxZHALO13Xx

    when i do vault open i get a null pointer on line 55 which is p.openInventory(backpacks.get(p.getName()));

    heres the code for that stuff

    Command
    Code:
      if (cmd.equalsIgnoreCase("open")){
                        if(args.length == 1){        // |
                            //Error NullPointer Line 55 \/
                            if(backpacks != null) {
                                if(p.getName() != null) {
                                        p.openInventory(backpacks.get(p.getName()));
                                    }
                            }
                        }
                        else {
                            p.sendMessage(MOREARGS);
                        }
                    }
    HashMap
    Code:
     private HashMap<String, Inventory> backpacks = new HashMap<String, Inventory>();
    Listeners
    Code:
     @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Inventory inv = Bukkit.getServer().createInventory(e.getPlayer(), 27, ChatColor.GREEN + e.getPlayer().getName() + "'s Vault");
    
            if (plugin != null) {
                if (plugin.getConfig() != null) {
                    if (plugin.getConfig().contains("backpacks." + e.getPlayer().getName())) {
                        for (String item : plugin.getConfig().getConfigurationSection("backpacks." + e.getPlayer().getName()).getKeys(false)) {
                            inv.addItem(loadItem(plugin.getConfig().getConfigurationSection("backpacks." + e.getPlayer().getName() + "." + item)));
                        }
                    }
                    backpacks.put(e.getPlayer().getName(), inv);
                }
            }
        }
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            if (!plugin.getConfig().contains("backpacks." + e.getPlayer().getName())) {
                plugin.getConfig().createSection("backpacks." + e.getPlayer().getName());
            }
    
            char c = 'a';
            for (ItemStack itemStack : backpacks.get(e.getPlayer().getName())) {
                if (itemStack != null) {
                    saveItem(plugin.getConfig().createSection("backpacks." + e.getPlayer().getName() + "." + c++), itemStack);
                }
            }
    
            plugin.saveConfig();
        }
    
        private void saveItem(ConfigurationSection section, ItemStack itemStack) {
            section.set("type", itemStack.getType().name());
            section.set("amount", itemStack.getAmount());
            // Save more information.
        }
    
        private ItemStack loadItem(ConfigurationSection section) {
            return new ItemStack(Material.valueOf(section.getString("type")), section.getInt("amount"));
            // Load more information.
        }
    }
    
     
  2. Offline

    ChipDev

    Does backpack contain player?
     
  3. Offline

    Rocoty

    Why didn't you post the error? I can GUESS that backpacks doesn't have an Inventory mapped to the player name in question.
     
Thread Status:
Not open for further replies.

Share This Page