Solved Null ;p

Discussion in 'Plugin Development' started by CraftCreeper6, May 12, 2015.

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

    CraftCreeper6

    Hii, again... Lots of null things lately...
    So, before you say "Add a null check". Already did - gives me an NPE (Don't ask why, because I don't know...)

    My code:
    Code:
        @EventHandler
        public void onMove(PlayerMoveEvent e)
            {
            Player player = e.getPlayer();
            String uuid = player.getUniqueId().toString();
            String quest = qm.getQuest(uuid);
            String questName = ChatColor.stripColor(quest);
            int x = main.travel.getInt(questName + ".X");
            int z = main.travel.getInt(questName + ".Z");
            Bukkit.broadcastMessage("Test " + x + " " + z + "    " + questName);
            if(player.getLocation().getX()>x-1&&player.getLocation().getX()<x+1 &&
               player.getLocation().getZ()>z-1&&player.getLocation().getZ()<z+1);
                {
                Bukkit.broadcastMessage("Test1");
                main.giveReward(questName, player);
                Bukkit.broadcastMessage("Test2");
                }
           
            }
    qm.getQuest(uuid);
    Code:
    private HashMap<String, String> currentQuest = new HashMap<String, String>();
       
        public void add(String uuid, String questName)
            {
            if(currentQuest.containsKey(uuid)) return;
            currentQuest.put(uuid, questName);
            }
       
        public void remove(String uuid)
            {
            if(!currentQuest.containsKey(uuid)) return;
                currentQuest.remove(uuid);
            }
       
        public String getQuest(String playerUUID)
            {
            return currentQuest.get(playerUUID);
            }
    When a player clicks on an Item in an inventory it changes their current quest and therefore removes their previous quest. I want this, however, the method "getQuest()" is returning null, I know I am in there as I printed out the contents ;/

    Any help is appreciated!
     
  2. Offline

    mine-care

    Does currentQuest contain the players uuid? If it doesn't that's why it returns null also your npe would be very helpful.
     
  3. Offline

    CraftCreeper6

    @mine-care
    Yes, it contains it.
    EDIT:
    Caused by: java.lang.NullPointerException
    at com.outlook.quests.jordna.listeners.TravelQuest.onMove(TravelQuest.ja
    va:32) ~[?:?]

    Line 32:
    if(questName.equals(null)) return;
     
  4. This is literally impossible. If you ever manage to make it happen, contact Oracle or drop everything and spend time with those you love, because hell has frozen over, or an apocalypse has arrived, or any other unlikely even has happened :)

    In this case, you have not done a proper null check. Your null check with either be false or will throw an NPE - you can't call methods on null references ;) Additionally, your first if statement in the OP is broken.
     
    mine-care likes this.
  5. Offline

    CraftCreeper6

    @AdamQpzm
    How do I check if it is null then? ;')

    EDIT: I know, gotta fix this first though ;p

    EDIT: Tried .isEmpty() etc... Nothing ;p
     
    Last edited: May 12, 2015
  6. Offline

    mine-care

    @CraftCreeper6
    Object#equals(Object param) is a method so if it is null its like saying if(null.equals(value)) that cant work since null is absolute nothingness. So instead use the == operator to compare with null.
     
  7. Offline

    CraftCreeper6

    @mine-care
    I used the opposite: if(questName!=null&&!questName.isEmpty())
     
  8. Offline

    mine-care

    ???
     
  9. Offline

    stormneo7

    You never check if the list contains the uuid ever in your code...
     
  10. Offline

    CraftCreeper6

    @stormneo7
    Printed out the contents, no need, plus, I do. I just don't show the code, it's a check in one of my methods.


    @mine-care
    I just used the opposite, it's the same as what you said, but the exact opposite...
     
  11. Offline

    Gamecube762

    If questName is null, you will still get an NPE on that line since you are USING something that is already null.

    To fix, use 2 different if statements.
     
  12. Offline

    CraftCreeper6

    @Gamecube762
    It's already fixed ;p I'm adding checks as we speak.
     
  13. @Gamecube762 Uhh.. no? That's what the short-circuit operation is useful for - if the questName is null, then the first condition will be false. It won't bother to execute the rest, because that would be pointless.
     
  14. Offline

    Gamecube762

    @AdamQpzm I've had problems with NPEs and if statements before, and the solution I found was listed above.

    I may have to do some testing with it then.
     
  15. mine-care and Gamecube762 like this.
Thread Status:
Not open for further replies.

Share This Page