Solved getType() null?

Discussion in 'Plugin Development' started by webbhead, Mar 27, 2016.

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

    webbhead

    Code:
    Code:
        public static double getInventoryWorth(Player p) {
            double worth = 0.0;
            Inventory inv = p.getInventory();
            ItemStack[] contents = inv.getContents();
    
            for (ItemStack item : contents) {
                if (item.getType() == Material.STONE) {
                    worth = worth + sellStone;
                } else if (item.getType() == Material.COBBLESTONE) {
                    worth = worth + sellCobble;
                } else if (item.getType() == Material.EMERALD) {
                    worth = worth + sellEmerald;
                } else if (item.getType() == Material.EMERALD_ORE) {
                    worth = worth + sellEmerald_ore;
                } else if (item.getType() == Material.EMERALD_BLOCK) {
                    worth = worth + sellEmerald_block;
                } else if (item.getType() == Material.IRON_INGOT) {
                    worth = worth + sellIron;
                } else if (item.getType() == Material.IRON_ORE) {
                    worth = worth + sellIron_ore;
                } else if (item.getType() == Material.IRON_BLOCK) {
                    worth = worth + sellIron_block;
                } else if (item.getType() == Material.DIAMOND) {
                    worth = worth + sellDiamond;
                } else if (item.getType() == Material.DIAMOND_ORE) {
                    worth = worth + sellDiamond_ore;
                } else if (item.getType() == Material.DIAMOND_BLOCK) {
                    worth = worth + sellDiamond_block;
                } else if (item.getType() == Material.GOLD_INGOT) {
                    worth = worth + sellGold;
                } else if (item.getType() == Material.GOLD_ORE) {
                    worth = worth + sellGold_ore;
                } else if (item.getType() == Material.GOLD_BLOCK) {
                    worth = worth + sellGold_block;
                } else {
                    worth = worth;
                }
            }
    
            return worth;
        }
    I get a null pointer when ever i try to compare item.getType() to Material.<MaterialType> any reasons why?
     
  2. Offline

    Zombie_Striker

    @webbhead
    You have not optimized this bit of code yet, correct? You may want to either consider using a switch statement or storing the worth and the material in a Map.

    Are you such item is not null? .getContents returns null objects if the slot is empty.
     
  3. Offline

    webbhead

    Shoot yep I will use a switch also, i forgot to paste in the null/air check which is in the code.
     
  4. Offline

    ChazSchmidt

    What line produces the null pointer? Just curious
     
Thread Status:
Not open for further replies.

Share This Page