Solved Please help me "java.lang.NullPointerException"

Discussion in 'Plugin Development' started by Zen3515, Nov 19, 2013.

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

    Zen3515

    My Server Log
    Code:
    2556-11-19 20:07:50 [SEVERE] Could not pass event PlayerToggleSneakEvent to FiFi v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:1029)
        at net.minecraft.server.v1_6_R3.Packet19EntityAction.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.Zen3515.FiFi.FiFi.onPlayerToggleSneakEvent(FiFi.java:99)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 14 more
    ↓HashMapPlayerDataManager Class↓
    Code:java
    1. public HashMap<String, Boolean> HashMapPlayerFiFiStatus = new HashMap<String, Boolean>();
    2. public boolean isFiFiEnable(Player player)
    3. {
    4. player.sendMessage("We are in isFiFiEnable");
    5. String playerName = player.getName();
    6. if (HashMapPlayerFiFiStatus.containsKey(playerName))
    7. {
    8. return HashMapPlayerFiFiStatus.get(playerName);
    9. }
    10. else
    11. {
    12. HashMapPlayerFiFiStatus.put(playerName, false);
    13. return false;
    14. }
    15. }


    ↓My main Class↓ (Line 99 is "if (HashMapPlayerData.isFiFiEnable(player))")
    Code:java
    1. private HashMapPlayerDataManager HashMapPlayerData;
    2. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
    3. public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent e) {
    4. Player player = e.getPlayer();
    5. /************************************************************************************************************/
    6. player.sendMessage("We are in Tooglesneak event");
    7. if (HashMapPlayerData.isFiFiEnable(player)) <————————————————————————Line 99 is here
    8. {
    9. ........
    10. }
    11. else
    12. {
    13. player.sendMessage("WTF");
    14. }
    15. }


    When I press shift I will get
    "We are in Tooglesneak event"
    and nothing....

    please help me this is my first time for using HashMap.
     
  2. Offline

    L33m4n123

    which is your line 99 in onPlayerToggleSneakEvent
     
  3. Offline

    Zen3515

    if(HashMapPlayerData.isFiFiEnable(player))<————————————————————————Line99 is here
    I have wrote in my main class
     
  4. Offline

    FatAussieFatBoy

    Zen3515

    Code:java
    1. if (HashMapPlayerData.isFiFiEnable(player)) <————————————————————————Line 99 is here
    2. {
    3. ........
    4. }
    5.  
    6.  


    Where is the Code for what the Player does? I am assuming there is one there, but your not willing to show us?
     
  5. Offline

    Zen3515

    I don't have to show my code there,
    Because I have tried plyer.sendMessage("We are here2"); at the first line of that conditions, but I doesn't recive any message. It must be line 99.
     
  6. Offline

    FatAussieFatBoy

    Zen3515
    Sorry, but I have no Idea what could be the Problem.
    When I wake up tomorrow I will have another look.
    But for now, I have no Idea what it could be.

    Zen3515
    Just had a Quick look over it again before I go to bed, and I realized that your Line 99 isn't calling anything, it is only seeing if HashMapPlayerData.isFiFiEnable() is equal to Player but isFiFiEnable only handles if the Player is in the HashMapFiFiStatus or not, there is no code in the line 99 that tells the Plugin to check if the Player is set to false or true. I have no idea how you would code this, but if I had to take a Random Guess, I would say that you need a Checker in your Line 99 to see if the isFiFiEnable() Boolean is true or false for the Player.

    - FatAussieFatBoy

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    Zen3515 likes this.
  7. Offline

    1Rogue Retired Staff

    Looks like "HashMapPlayerData" is null / uninitialized.
     
    NathanWolf likes this.
  8. Offline

    sgavster

    It's because it's null, as 1Rogue said. Please show us your HashMap.
     
  9. Offline

    FatAussieFatBoy

    Zen3515
    As 1Rogue & sgavster have said, your HashMapPlayerData is null, so If you could please Paste your HashMapPlayerDataManager HashMap, then we would be able to help you figure out the Problem.
    Cheers -
    - FatAussieFatBoy
     
  10. Offline

    1Rogue Retired Staff


    He already did, he just needs to initialize it...
     
  11. Offline

    FatAussieFatBoy

    1Rogue
    I don't believe he has given us the HashMapPlayerDataManager HashMap, as the Only HashMap he has given us above is the HashMapPlayerFiFiStatus HashMap, and Inside the HashMapPlayerFiFiStatus it does not mention or even point towards the HashMapPlayerDataManager. If I am wrong in saying this please feel free to Correct me, as I am only just starting to wake-up properly.
    Cheers -
    - FatAussieFatBoy
     
  12. Offline

    1Rogue Retired Staff

    The third class he provided:
    Code:java
    1. private HashMapPlayerDataManager HashMapPlayerData;
     
  13. Offline

    FatAussieFatBoy

    1Rogue
    But he still isn't showing us what HashMapPlayerDataManager is, He is only telling us that HashMapPlayerDataManager is being nicknamed to HashMapPlayerData. He would need to have a Separate Class/HashMap that would be HashMapPlayerDataManager or this Line would have a Error.

    EDIT : I am unaware if HashMapPlayerDataManager is a actual Bukkit Code or not (never have heard of it before)
     
  14. Offline

    Zen3515

    Yes, I have given all code about that map.
    How to initialize it ?
    did it mean that I have to set some default value at "public HashMap<String, Boolean> HashMapPlayerFiFiStatus = new HashMap<String, Boolean>();"
     
  15. Offline

    1Rogue Retired Staff

    Yes. If you are using Java 7 then you don't need the second generic pair.
     
  16. Offline

    Zen3515

    1Rogue
    I still can't use it

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
    2. public void onPlayerJoin(PlayerJoinEvent e)
    3. {
    4. Player player = e.getPlayer();
    5. [COLOR=#ff0000]HashMapPlayerData.CreateAllMap(player); "<---------ProblemHere"[/COLOR]
    6. }


    Code:java
    1. public void CreateAllMap(Player player)
    2. {
    3. String playerName = player.getDisplayName();
    4. HashMapPlayerFiFiStatus.put(playerName, false);
    5. player.sendMessage("Created data");
    6. }



    This below code doesn't work
     
  17. Offline

    Garris0n

    First of all, this. Second, your isFiFiEnable() method isn't static, so where is your instance of your HashMapPlayerData class, or whatever that class is called. But really, fix your variable/method names, it makes it very hard to understand what's going on in the code.
     
    Zen3515 likes this.
  18. Offline

    Zen3515

    Thanks, I have to add static all of them. and it will working
    But my compiler said "The static field______should be accessed in a static way "
    How to fix this ?
     
  19. Offline

    Garris0n

    By accessing it in a static way. As I said, between the amount of code I can see and the way the variables are named I have no idea how to help you...

    Try reading through this:
    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
     
    Zen3515 likes this.
  20. Offline

    1Rogue Retired Staff

  21. Offline

    Garris0n

    Zen3515 likes this.
  22. Offline

    1Rogue Retired Staff


    Yes, as long as people know what they're doing. For beginners, it is best to avoid them overall.
     
  23. Offline

    geNAZt

    Its better in the Bukkit enviroment to not use static variables at all. Since if the Pluginmanager decides to reload the plugins it can happen that the GC will never clear out the old Plugin Instance
     
  24. Offline

    1Rogue Retired Staff


    There is a place for static in bukkit plugins but it isn't for easy access to variables.
     
  25. Offline

    geNAZt

    Yes cases like static finals acting as constants for example. But you should never store any ref from bukkit inside a static field.
     
  26. Offline

    Zen3515

    So if I use them on the same class, it will work?
     
Thread Status:
Not open for further replies.

Share This Page