Solved Puzzling NPE on Non-Null String

Discussion in 'Spigot Plugin Development' started by WAS, May 24, 2017.

  1. WAS

    WAS

    I'm a little confused. Getting a NPE on a non-null string.

    Code (Java):
         String type = (String) map.get("type");
         String roomName = (String) map.get("room");

         logger.info("Checking room: " + roomName);
         if (!(chatRooms.roomExists(roomName))) { // Line 61 : roomName null
           player.sendMessage(Utilities.beautify(config,
               config.getString("local.room-does-not-exist", "&cThe room &r&l{ROOM}&r&c doesn't exist!")));
           update(sign);
           return;
         }
    Code (Text):

    [11:51:56 INFO]: Checking room: WelcomeLounge
    [11:51:56 ERROR]: Could not pass event PlayerInteractEvent to WeChat v0.3.7 BETA
    org.bukkit.event.EventException: null
    ...
    Caused by: java.lang.NullPointerException
      at wa.was.wechat.db.ChatSigns.action(ChatSigns.java:61) ~[?:?]
     
    #1 WAS, May 24, 2017
    Last edited: May 24, 2017
  2. Is the "chatRooms" variable null? Why do you think "roomName" is null and not "chatRooms"?
     
    • Agree Agree x 2
  3. WAS

    WAS

    That's a good point, if the same global variable isn't used initially to create the room sign and make sure the room exists before placing the sign.
     
  4. chatRooms is null, as that is the only dereferenced variable at that line (and the stack trace stops there).
     
    • Like Like x 1
  5. WAS

    WAS

    How is the global variable not null one second, and null the next, when it's instantiated once? Interesting. As again, you can place a sign and it checks the room just fine. Going to poke around.

    Update: I see what happened. Renaming "ChatRooms" to "chatRooms" from a old version of the class where "ChatRooms" was using a static reference was resetting the variable with nothing in another function.