NullPointer

Discussion in 'Spigot Plugin Development' started by Sreppy, May 3, 2017.

  1. Hey guys, so I was wondering what was causing me to get a null pointer here?

    Code (Text):

    @EventHandler
    public void onSpawnerPlace(BlockPlaceEvent e) {
        Block b = e.getBlockPlaced();
        ItemStack inh = e.getPlayer().getInventory().getItemInMainHand();
            if (b != null && inh != null) {
                e.getPlayer().sendMessage("1");
                if (b.getType() == Material.MOB_SPAWNER && inh.getType() == Material.MOB_SPAWNER) {
                    ItemMeta im = inh.getItemMeta();
                    e.getPlayer().sendMessage("2");
                    if (inh.getType() != Material.MOB_SPAWNER){
                        e.getPlayer().sendMessage("6");
                    }
                    if (im.getDisplayName() == null){
                        e.getPlayer().sendMessage("5");
                    }
          // Null Pointer          if (!im.getDisplayName().contains(translate("Zombie"))) {
                        e.getPlayer().sendMessage("3");
                        if (im.getDisplayName().contains(translate("Zombie"))) {
                            e.getPlayer().sendMessage("4");
                            b.setType(Material.MOB_SPAWNER);
                            setSpawner(b, EntityType.ZOMBIE);

                        }
     

    It will only send me the numbers 1 and 2 whenever I place the spawner.

    Help would be very much appreciated, cheers!
     
  2. The item has no display name.

    EDIT: didnt see the check. Can we see your translate method?
     
  3. Use ItemMeta#hasDisplayName() to check if a display name is present before trying to get the display name.
     
  4. This is the whole thing:
    Code (Text):

    @EventHandler
    public void onSpawnerPlace(BlockPlaceEvent e) {
        Block b = e.getBlockPlaced();
        ItemStack inh = e.getPlayer().getInventory().getItemInMainHand();
        if (!inh.getItemMeta().hasDisplayName()) {
            return;
        }
            if (b != null && inh != null) {
                e.getPlayer().sendMessage("1");
                if (b.getType() == Material.MOB_SPAWNER && inh.getType() == Material.MOB_SPAWNER) {
                    ItemMeta im = inh.getItemMeta();
                    e.getPlayer().sendMessage("2");
                    if (inh.getType() != Material.MOB_SPAWNER){
                        e.getPlayer().sendMessage("6");
                    }
                    if (im.getDisplayName() == null){
                        e.getPlayer().sendMessage("5");
                    }
                    if (!im.getDisplayName().contains(translate("Zombie"))) {
                        e.getPlayer().sendMessage("3");
                        if (im.getDisplayName().contains(translate("Zombie"))) {
                            e.getPlayer().sendMessage("4");
                            b.setType(Material.MOB_SPAWNER);
                            setSpawner(b, EntityType.ZOMBIE);

                        }
                    } else if (im.getDisplayName().contains(translate("Skeleton"))) {
                        b.setType(Material.MOB_SPAWNER);
                        setSpawner(b, EntityType.SKELETON);
                    }
                }
            }
        }
     
    Here's the translate:
    Code (Text):

    public String translate(String msg) {
        return ChatColor.translateAlternateColorCodes('&', msg);

    }
     
     
  5. Okay, I know this doesn't contribute to your issue but,
    the second if statement has no use because you already checked if the held item was mob spawner material
     
  6. You're checking whether the item has a display name, however, you're just continuing even if that is the case; add a return statement in the if body.
     
  7. He mentioned that "5" doesn't appear in the chat.
     
  8. Oops, sorry, my mistake. But then this doesn't make any sense. 'im' is not null since then the NPE would be thrown in this if statement, and the display name is also present (otherwise you'd see the 5). And translate("Zombie") can't return null either (except if ChatColor#translateAlternateColorCodes can return null, I don't know about that).
     
  9. You could add an else statement to the if(item.getDisplayname() == null) and then add the code to check the name there. Or you could add a " return" into that statement.
     
  10. Strahan

    Benefactor

    Of course 5 won't appear, he returns if there is no displayname earlier in the code before the check for displayname and the subsequent "5" if it has none. You have a bunch of redundant logic and a few that just don't make sense.

    If I were to make this functionality, I'd do it this way:

    Code (Text):
    @EventHandler
    public void onSpawnerPlace(BlockPlaceEvent e) {
      Block b = e.getBlockPlaced();
      ItemStack inh = e.getPlayer().getInventory().getItemInMainHand();
      if (b != null && inh != null) {
          if (!inh.getItemMeta().hasDisplayName()) return;
          if (b.getType() == Material.MOB_SPAWNER && inh.getType() == Material.MOB_SPAWNER) {
            ItemMeta im = inh.getItemMeta();
            String desiredET = ChatColor.stripColor(im.getDisplayName().toUpperCase());
            if (this.getConfig().getStringList("valid-spawners").contains(desiredET.toLowerCase())) {
              try {
                EntityType et = EntityType.valueOf(desiredET);
                ((CreatureSpawner)b.getState()).setSpawnedType(et);
                e.getPlayer().sendMessage("Spawner configured!");
              } catch (IllegalArgumentException ex) {
                e.getPlayer().sendMessage("Invalid entity type!");
              }
            } else {
              e.getPlayer().sendMessage("This spawner is set to a disallowed type, it cannot be activated.");
            }
        }
      }
    }
    That way I don't have to evaluate the display name for each entity type, it's automatic. The spawner just needs to have the displayname match a valid EntityType and be in the config's list of allowed types.
     
    #10 Strahan, May 3, 2017
    Last edited: May 4, 2017
    • Agree Agree x 2
  11. WAS

    WAS

    What is the NPE? We're all just speculating at your code, but your NPE should pinpoint the line...

    Update Oh I see your comment. Which is weird. Though It's odd you are translating when you know there is no special characters. That is for translating color codes.

    Code (Java):
                    if (!im.hasDisplayName()){
                        e.getPlayer().sendMessage("5");
                        return;
                    }
                    if (!im.getDisplayName().toLowerCase().contains("zombie")) {
                        e.getPlayer().sendMessage("3");
                        // This following won't run because the above statement only runs if it doesn't have a display name containing zombie
                        if (im.getDisplayName().toLowerCase().contains("zombie")) {
                            e.getPlayer().sendMessage("4");
                            b.setType(Material.MOB_SPAWNER);
                            setSpawner(b, EntityType.ZOMBIE);
                            // If it is a zombie, you should be done here... return
                            return;
                        }
                    } else if (im.getDisplayName().toLowerCase().contains("skeleton")) {
     
    #11 WAS, May 4, 2017
    Last edited: May 4, 2017
    • Agree Agree x 1