What is wrong?

Discussion in 'Plugin Development' started by MordorKing78, Mar 4, 2015.

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

    MordorKing78

    I am trying to name my pet, What am I doing wrong?

    HashMap:

    Code:
        public HashMap<Player, Entity> petData = new HashMap<Player, Entity>();
    
    Adding the Pet:

    Code:
                        Chicken chicken = (Chicken) p.getWorld().spawnEntity(p.getLocation(), EntityType.CHICKEN);
                        petData.put(p, chicken);
    
    Setting its name:
    Code:
                LivingEntity pet = (LivingEntity)petData.get(p);
                String coloredMessage = args[1].replaceAll("&", "§");
                pet.setCustomName(coloredMessage);
                pet.setCustomNameVisible(true);    
    Error: Caused by: java.lang.NullPointerException
     
  2. Offline

    Skionz

  3. Offline

    Zombie_Striker

    At what line does it indicate that there's a nullpointer?

    It's bad idea to store the whole player in a map, just use their UUID. Also, instead of makeing a Chicken, make it an Entity (Since that map holds Entities)
     
    MrAwellstein likes this.
  4. @MordorKing78 In your situation, I'd personally store all of this data in per player config files instead of an ArrayList, because those will be cleared on reload/restart of the server.
     
  5. Offline

    MrAwellstein

    First, don't store Player in a hashmap, Store UUID or String (Player.getName()) (Use UUID if the hashmap will save ever, and use Player.getName() if it not going to persist past reload).

    Second, LivingEntity pet = (LivingEntity)petData.get(p); pet is probably null because the key, p, isn't in the hashmap.
     
    Last edited: Mar 4, 2015
  6. Offline

    MordorKing78

    @CodePlaysMinecraft Ik, But I just want to test it
    @Zombie_Striker the line pet.setCustomName(coloredMessage);
    @MrAwellstein I already have that

    I Changed Player to UUID but doesn't seem to change anything

    EDIT: Chaging Chicken to LivingEntity doesnt work either

    EDIT: This string puts out null too:

    Code:
                LivingEntity yourPet = (LivingEntity)petData.get(p.getUniqueId());
                Bukkit.broadcastMessage("This pet is very cool #" + yourPet);
                String coloredMessage = args[1].replaceAll("&", "§");
                yourPet.setCustomName(coloredMessage);
                yourPet.setCustomNameVisible(true);    
     
    Last edited: Mar 5, 2015
  7. Offline

    bazsi700

    EDIT: Sorry, I was blind, I don't know what's the matter.
     
  8. Offline

    MordorKing78

  9. Offline

    bazsi700

    @MordorKing78 Try to store as Chicken. I don't see any sense why wouldn't LivingEntity would work, but it worths a try :)
     
  10. Offline

    MordorKing78

  11. For Color Codes use:

    ChatColor.translateAlternateColorCodes('&', "Message")
     
  12. Offline

    MordorKing78

Thread Status:
Not open for further replies.

Share This Page