Solved Why is this creating an NPE

Discussion in 'Plugin Development' started by kameronn, Jan 2, 2017.

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

    kameronn

    i dont understand I do the same thing in other classes and it works fine

    Class thats creating the error:
    The line playerdata is returning null
    Code:
    DataManager datamanager = new DataManager();
    
        public void show(Player p) {
            PlayerData playerdata = datamanager.getData(p.getUniqueId());
    
            int listsize = playerdata.getPlayerData().getConfigurationSection("kits.size").getValues(false).entrySet()
                    .size(); // problem here
    
            ItemStack kitblock = new ItemStack(Material.MAGMA_CREAM, 1);
            ItemMeta kitblockim = kitblock.getItemMeta();
            Inventory kits = Bukkit.createInventory(p, InventoryType.HOPPER,
                    ChatColor.DARK_GRAY + "Kits for " + ChatColor.GREEN + ChatColor.BOLD + p.getName());
    
            ItemStack slimeball = new ItemBuilder(Material.SLIME_BALL, 1).setName(ChatColor.DARK_GRAY + "Unused Slot")
                    .toItemStack();
    
            for (int i = 0; i < listsize; i++) {
    
                int kitnamei = i + 1;
    
                String kitname = playerdata.getPlayerData().getString("kits.size.kit" + kitnamei + ".name");
                double abilitypoints = playerdata.getPlayerData()
                        .getDouble("kits.size.kit" + kitnamei + ".abilitypoints");
                kitblockim.setDisplayName(ChatColor.GREEN + kitname + "");
                ArrayList<String> lores = new ArrayList<String>();
                lores.add(ChatColor.GRAY + "Click to use the kit editor");
                lores.add(ChatColor.GRAY + "You used " + ChatColor.GREEN + abilitypoints + "/25.0" + ChatColor.GRAY
                        + " points");
                kitblockim.setLore(lores);
                kitblock.setItemMeta(kitblockim);
    
                kits.setItem(i, kitblock);
    
                while (!(kits.firstEmpty() == -1)) {
                    kits.setItem(kits.firstEmpty(), slimeball);
                }
    
                p.openInventory(kits);
            }
        }
    DataManager class:
    Code:
        HashSet<PlayerData> data = new HashSet<>();
       
        public HashSet<PlayerData> getHashSet() {
            return data;
        }
    
        public void addData(UUID uuid) {
            if (getData(uuid) == null) {
                PlayerData playerData = new PlayerData(uuid);
                data.add(playerData);
            }
    
        }
    
        public PlayerData getData(UUID uuid) {       
            for(PlayerData playerdata : data) {
                if(playerdata.getUUID().equals(uuid)) {
                    return playerdata;
                }
            }
           
            return null;
        }
    
        public boolean containsData(UUID uuid) {
            boolean contains = data.contains(uuid);
            return contains;
        }
    
        public Collection<PlayerData> getAllData() {
            return data;
        }
       
        public void removeData(UUID uuid) {
            if(containsData(uuid)) {
                data.remove(uuid);
            }
        }
    
    Before someone asks, yes that path does exists, and yes I am adding them to the hashmap. No I am not removing them for the hashmap ever (well not at least while they're on the server). What ends up happening is for some reason it doesnt think they're in the hashmap in the map while at the same time other methods work fine.
     
  2. Offline

    ShaneCraftDev

    If that line gives you an npe, PlayerData#getUuid might return null or something within that method does a call on a null object. You should post the stacktrace if you want more detailed help.
     
    Last edited: Jan 2, 2017
  3. Offline

    mythbusterma

    @ShaneCraftDev

    There is no null Object, null is the absence of value. Also, the return of getUniqueID being null wouldn't cause an NPE on that line, as it is just passing that as a parameter to the other method, although that could cause an issue, it wouldn't say the error is on that line.


    @kameronn

    The Player "p" is null. Make it not null.
     
  4. Offline

    ShaneCraftDev

    @mythbusterma
    I know what a null value represents.. I took on his word on the npe being thrown on that particulair line. He never showed the PlayerData class and I assumed either playerdata#getUuid returns a null value or something within that method does a call on a null object. I did not take in consideration of the Player field beign null.
     
  5. Offline

    kameronn

    @mythbusterma
    @ShaneCraftDev
    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
                if (event.getMaterial().equals(Material.BOOK)) {
                    if (event.getItem() != null) {
                        if (event.getItem().getItemMeta().getDisplayName().contains("Kit Creator")) {
                            PlayerData playerdata = datamanager.getData(player.getUniqueId());
    
                            kitsinventory = new KitsInventory();
                            kitsinventory.show(player);
                      
                        }
    
                    }
    
                }
    }
    it shouldnt be null

    The playerdata class is fine too because it works in other areas of my code, for example my scoreboard and killstreak system
    Also if i were to move this code to another class it would work

    EDIT: fixed itself i guess
     
    Last edited: Jan 2, 2017
Thread Status:
Not open for further replies.

Share This Page