Solved NullPointerException

Discussion in 'Plugin Development' started by JjPwN1, Nov 5, 2012.

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

    JjPwN1

    Code:
        @EventHandler
        public void respawnClasses(PlayerRespawnEvent e){
            if(getConfig().getBoolean("general.ban_until_next_round") == false){
                if(getConfig().getBoolean("maps.lobby.gameIsLobby") == false){
                    Player player = e.getPlayer();
                    if(getConfig().getInt("players." + player.getName() + ".class.helmet") != 0){
                        player.getInventory().getHelmet().setTypeId(getConfig().getInt("players." + player.getName() + ".class.helmet"));
                    }
                    if(getConfig().getInt("players." + player.getName() + ".class.chestplate") != 0){
                        player.getInventory().getChestplate().setTypeId(getConfig().getInt("players." + player.getName() + ".class.chestplate"));
                    }
                    if(getConfig().getInt("players." + player.getName() + ".class.leggings") != 0){
                        player.getInventory().getLeggings().setTypeId(getConfig().getInt("players." + player.getName() + ".class.leggings"));
                    }
                    if(getConfig().getInt("players." + player.getName() + ".class.boots") != 0){
                        player.getInventory().getBoots().setTypeId(getConfig().getInt("players." + player.getName() + ".class.boots"));
                    }
                    if(getConfig().getInt("players." + player.getName() + ".class.sword") != 0){
                        player.getInventory().getItemInHand().setTypeId(getConfig().getInt("players." + player.getName() + ".class.sword"));
                    }
                    if(getConfig().getInt("players." + player.getName() + ".class.sword") == 0){
                        player.getInventory().getItemInHand().setType(Material.WOOD_SWORD);
                    }
    This is my code for classes in a Team Deathmatch plugin. It says that the player.getInventory().getHelmet().setTypeId(getConfig().getInt("players." + player.getName() + ".class.helmet")); line (along with all the other lines that do that same thing) is a NullPointer in the console, when I afterwards caught the exception and printed the stack trace. Earlier it was saying it could not pass PlayerRespawnEvent to MCTDM. Anyways, my question is, how it this a nullpointer? The config has a number for each of the items, and yet it won't set the helmet
    Code:
    players: 
      JjPwN1:
        class:
          helmet: 298
          chestplate: 299
          leggings: 300
          boots: 301
    All help is appreciated.
     
  2. Offline

    Murderscene

    It's probably because if the player has no helmet then getHelmet returns null. Just a guess though.

    use setHelmet instead of getHelmet.setTypeID
     
  3. Offline

    Loogeh

    Yeah you have to do setHelmet, as Murderscene said.
    I haven't tested this but it should work
    Code:
    player.getInventory().setHelmet(Material.getMaterial(config.getInt(stuffhere)))
     
  4. Offline

    Ewe Loon

    Actually i think you have to do this
    player.getInventory().setHelmet(new ItemStack(.....));

    also, note as of 1.3 getting empty slots return a null ,
    before 1.3 they returned and air item
     
  5. Offline

    JjPwN1

    I found a workaround for this. Instead of item ids, I used strings, and if the strings equal something, set their armor as something. Example:
    Code:
                        if(getConfig().getString("players." + player.getName() + ".class.helmet").equalsIgnoreCase("lhelm")){
                            player.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                        }
    Actually, I used Loogeh 's method and it works. It's more efficient than strings, easier, and takes less time to code.
    Code:
    if(getConfig().getInt("players." + p.getName() + ".class.helmet") != 0){
    p.getInventory().setHelmet(new ItemStack(Material.getMaterial(getConfig().getInt("players." + p.getName() + ".class.helmet"))));
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. Offline

    Loogeh

    Hehe, glad I could help :D
     
Thread Status:
Not open for further replies.

Share This Page