Solved Not sure why my getInstance() returns null

Discussion in 'Plugin Development' started by Caedus, Sep 8, 2016.

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

    Caedus

    Hi, so i've done the same thing in other plugins but can't seem to figure it out, but essentially anything in my current plugin where I refer to a function in another class in a non static way, through getInstance(), doesn't seem to work.

    Error:
    Code:
    [00:23:44] [User Authenticator #4/INFO]: UUID of player IceFrog is 97252995-3f00-42c0-9ec7-4901a2bb59ef
    [00:23:44] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to ColonyWars v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:142) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:115) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:745) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at me.caedus.Events.PlayerJoinListener.onPlayerJoinEvent(PlayerJoinListener.java:19) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 14 more
    [00:23:44] [Server thread/INFO]: IceFrog[/127.0.0.1:58272] logged in with entity id 205 at ([world]113.25310587123857, 76.5, 244.2688810460275)
    [00:23:44] [User Authenticator #7/INFO]: Disconnecting /127.0.0.1:58269: Failed to verify username!
    My PlayerJoin class:
    Code:
    package me.caedus.Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.ColonyWars.ColonyWars;
    import me.caedus.GameManager.GameManager;
    
    public class PlayerJoinListener implements Listener {
       
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
            CWPlayer cwp = new CWPlayer(p);
            if(p.getName().equals("IceFrog")){
                //GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.blue);
                Bukkit.broadcastMessage(""+cwp.getTeam().getColour()+" "+cwp.getTeam().getTeamBalance()+" "+ColonyWars.getInstance().getS());
               
            } else {
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getRedTeam());
            }
            GameManager gm = GameManager.getInstance();
            Bukkit.broadcastMessage("Team: "+ GameManager.getInstance().getPlayerToCWPlayer().get(p).getTeam().getColour());
            CWPlayer cwpp = gm.getPlayerToCWPlayer().get(p);
            Bukkit.broadcastMessage("name test: "+cwpp.getPlayer().getName()+" "+cwpp.getBalance());
            Bukkit.broadcastMessage("Team Balance: "+ gm.getPlayerToCWPlayer().get(p).getTeam().getTeamBalance());
        }
    }
    
    My GameManager class:
    Code:
    package me.caedus.GameManager;
    
    import java.util.HashMap;
    
    import org.bukkit.entity.Player;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.ColonyWars.CWTeam;
    import me.caedus.ColonyWars.ColonyWars;
    
    public class GameManager {
       
        //private static GameManager instance;
        @SuppressWarnings("unused")
        private static ColonyWars plugin;
        public static HashMap<Player, CWPlayer> p2cwp;
       
        private CWTeam red = new CWTeam("Red");
        public static CWTeam blue = new CWTeam("Blue");
        private CWTeam white = new CWTeam("White");
        private CWTeam black = new CWTeam("Black");
       
        public GameManager(){
           
        }
       
        public HashMap<Player, CWPlayer> getPlayerToCWPlayer(){
            return p2cwp;
        }
        public static GameManager getInstance(){
            return new GameManager();
        }
        public CWTeam getRedTeam(){
            return red;
        }
        public CWTeam getBlueTeam(){
            return blue;
        }
        public CWTeam getWhiteTeam(){
            return white;
        }
        public CWTeam getBlackTeam(){
            return black;
        }
    }
    
    I've done this before in other plugins with success, I feel like i'm missing something really easy here.

    Cheers
     
  2. Offline

    Zombie_Striker

    Something is null at line 19. I can't see why this would throw an NPE though. Try going through all the variables on line 19 and see if any of them are null.
     
  3. Offline

    Caedus

    Hold up, this is the current error log for the following PlayerJoin:

    Error:
    Code:
    [01:21:12] [User Authenticator #2/INFO]: UUID of player IceFrog is 97252995-3f00-42c0-9ec7-4901a2bb59ef
    [01:21:12] [Server thread/INFO]: Blue 0[m
    [01:21:12] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to ColonyWars v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:142) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:115) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:745) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at me.caedus.Events.PlayerJoinListener.onPlayerJoinEvent(PlayerJoinListener.java:27) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 14 more
    [01:21:12] [Server thread/INFO]: IceFrog[/127.0.0.1:58992] logged in with entity id 216 at ([world]113.25310587123857, 76.5, 244.2688810460275)
    [02:11:52] [Server thread/INFO]: CONSOLE: [0;31;1mPlease note that this command is not supported and may cause issues when using some plugins.[m
    [02:11:52] [Server thread/INFO]: CONSOLE: [0;31;1mIf you encounter any issues please use the /stop command to restart your server.[m
    [02:11:52] [Server thread/INFO]: [Factions] Disabling Factions v2.8.19[/CODE
    
    And PlayerJoinListener: 
    [CODE]package me.caedus.Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.GameManager.GameManager;
    
    public class PlayerJoinListener implements Listener {
       
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
            CWPlayer cwp = new CWPlayer(p);
            if(p.getName().equals("IceFrog")){
                //GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.blue);
                Bukkit.broadcastMessage(""+cwp.getTeam().getColour()+" "+cwp.getTeam().getTeamBalance());
               
            } else {
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getRedTeam());
            }
            GameManager gm = GameManager.getInstance();
            Bukkit.broadcastMessage("Team: "+ GameManager.getInstance().getPlayerToCWPlayer().get(p).getTeam().getColour());
            CWPlayer cwpp = gm.getPlayerToCWPlayer().get(p);
            Bukkit.broadcastMessage("name test: "+cwpp.getPlayer().getName()+" "+cwpp.getBalance());
            Bukkit.broadcastMessage("Team Balance: "+ gm.getPlayerToCWPlayer().get(p).getTeam().getTeamBalance());
        }
    }
    
     
  4. Offline

    Zombie_Striker

    One of these two lines should be null.Null check everything on those lines and find out what is null. [edid] The hashmap is null because you have not set it to anything. Make sure p2cwp is set to something before using it.


    BTW: Fix this:
    This will always override the previous instance. This is a bad thing. Instead, set "instance" equal to a new GameManager, and return "instance".
     
  5. Offline

    Caedus

    Cheers for helping
    @Zombie_Striker :)

    Unfortunately, I still seem to have the same problem. If I understood you correctly, i've corrected the hashmap as you instructed:

    Code:
    package me.caedus.GameManager;
    
    import java.util.HashMap;
    
    import org.bukkit.entity.Player;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.ColonyWars.CWTeam;
    
    public class GameManager {
       
        private static GameManager instance;
        private HashMap<Player, CWPlayer> p2cwp = new HashMap<Player, CWPlayer>();
       
        private CWTeam red = new CWTeam("Red");
        private CWTeam blue = new CWTeam("Blue");
        private CWTeam white = new CWTeam("White");
        private CWTeam black = new CWTeam("Black");
       
        public GameManager(){
           
        }
       
        public HashMap<Player, CWPlayer> getPlayerToCWPlayer(){
            return p2cwp;
        }
        public static GameManager getInstance(){
            return instance;
        }
        public CWTeam getRedTeam(){
            return red;
        }
        public CWTeam getBlueTeam(){
            return blue;
        }
        public CWTeam getWhiteTeam(){
            return white;
        }
        public CWTeam getBlackTeam(){
            return black;
        }
    }
    
    Error:
    Code:
    [03:02:35] [User Authenticator #2/INFO]: UUID of player IceFrog is 97252995-3f00-42c0-9ec7-4901a2bb59ef
    [03:02:35] [Server thread/INFO]: Blue 0[m
    [03:02:35] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to ColonyWars v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:142) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:115) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:745) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at me.caedus.Events.PlayerJoinListener.onPlayerJoinEvent(PlayerJoinListener.java:28) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 14 more
    [03:02:35] [Server thread/INFO]: IceFrog[/127.0.0.1:60526] logged in with entity id 206 at ([world]106.22947191552016, 76.5, 240.0609109871841)
    [03:02:49] [Server thread/INFO]: Stopping the server
    And PlayerJoin:
    Code:
    package me.caedus.Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.GameManager.GameManager;
    
    public class PlayerJoinListener implements Listener {
       
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
            CWPlayer cwp = new CWPlayer(p);
            if(p.getName().equals("IceFrog")){
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getBlueTeam());
                Bukkit.broadcastMessage(""+cwp.getTeam().getColour()+" "+cwp.getTeam().getTeamBalance());
               
            } else {
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getRedTeam());
            }
            GameManager gm = GameManager.getInstance();
            Bukkit.broadcastMessage("Team: "+ GameManager.getInstance().getPlayerToCWPlayer().get(p).getTeam().getColour());
            CWPlayer cwpp = gm.getPlayerToCWPlayer().get(p);
            Bukkit.broadcastMessage("name test: "+cwpp.getPlayer().getName()+" "+cwpp.getBalance());
            Bukkit.broadcastMessage("Team Balance: "+ gm.getPlayerToCWPlayer().get(p).getTeam().getTeamBalance());
        }
    }
    
    So I feel like i'm getting the same error, even though I think i've done the hashmap thing that you thought might have been the problem?
     
  6. Offline

    Zombie_Striker

    1. The value from the hashmap can be null.
    2. The team can be null.
    One of these two things is what is causing the error. Make sure the player is inside the hashmap before using this line. If that does not fix the problem, make sure team it is not null.
     
  7. Offline

    ShaneCraftDev

    I think the key here is for you to learn basic java knowledge. Your singleton of GameManager never get's set. You create a static instance of this class, but it is never initialized. For a singleton pattern you should make the class final and the constructor private. A better way of creating singletons in java is by using enums instead:

    Code:java
    1.  
    2. public enum Singleton {
    3.  
    4. INSTANCE;
    5.  
    6. // fields
    7.  
    8. // methods
    9.  
    10. }
    11.  
     
  8. Offline

    timtower Moderator Moderator

    @ShaneCraftDev Using an enum for something that should be a class isn't really the way to go though.
     
    DoggyCodeâ„¢ likes this.
  9. Offline

    ShaneCraftDev

    @timtower You should point out why. There are downsides to any implementations of a singleton pattern, how ever enums are the best approach for singletons because this approach is thread safe. Having multiple threads might create their own instance of a singleton implemented by a class per thread, how rare this might be.

    Here is more on implementing singleton patterns by Joshua Bloch.
     
  10. Offline

    I Al Istannen

    @timtower
    An enum is a "normal" class. You can do what you would do with a class, but you get the singelton contract for free.

    If it should be a singelton, I would agree with Joshua Bloch (just read effective java a few days ago :p) and @ShaneCraftDev and make it an enum.

    The normal singelton pattern (private static instance) works too, but there are a few things you need to be aware of to guarantee the contract (only one instance), whereas the jvm does that for you if you use an enum.

    @timtower
    Could you elaborate a bit why you think it is not a good idea?
    I do not know everything and certainly less than you. I would appreciate if you could write down your reasoning :)
     
  11. Offline

    timtower Moderator Moderator

    @I Al Istannen Try to extend an enum to add new functions.
    You can't while you might want to add utility functions using the same singleton but casted to the new type that you made.
     
  12. Offline

    ShaneCraftDev

    Any proper singleton should have a private constructor. They are not intended to be extended. If you intent to extend singletons, you should not use singletons.
     
  13. Offline

    timtower Moderator Moderator

    Enums aren't meant to be as classes either though.
    And the question can also be: why would you need a singleton? And if you have one why don't you make it a static class all together?
     
  14. Offline

    ShaneCraftDev

    @timtower If I further extend your question, Why shouldn't you use public [static] final fields in your plugin class for this small plugin.

    That was not the question, OP is trying to implement a singleton. I stated it is better to use enums for java, because they're thread safe, where singleton implementations using classes are not.
     
  15. Offline

    I Al Istannen

    @timtower
    True. It is probably not needed though.

    Just add them to the enum class?

    Then you probably know beforehand that you might want to extend it and make it a proper abstract class or interface.


    I don't know.

    You only want one GameManger, no matter what happens. You can make it a field of the main class and initialize it on onEnable, but you can also make it a singleton. I would prefer making it a field, but others may choose the singelton.


    Not quite sure what you mean with that. You mean making all methods and fields static? Implementing interfaces/extending other classes, passing the instance around, so you can later decide to pass another one as well as making it theoretically (if you don't use an enum) changeable (ToolKit#getToolkit()).
    And you can serialize it and later restore it, if it has a state worth preserving.
    That is easier with an enum, if you want to protect against the serialization methods to secretly create another instance. Though that is far fetched :p

    But I think I see where you are coming from.
    I would probably still prefer an enum for a simple singelton, which is unlikely to change. Though for other taks, the strategy pattern may be nicer or the singelton you mentioned.
     
  16. Offline

    Caedus

    @Zombie_Striker

    I checked for the hashmap being null, it just throws an error without printing the result of my nullcheck. I also checked for getting a Team but it throws an error before printing anything. However when I nullcheck for GameManager.getInstance() it does the null check and prints "breh", as it should if it's null.

    Code:
    [12:31:25] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to ColonyWars v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:142) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:115) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:745) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
        at me.caedus.Events.PlayerJoinListener.onPlayerJoinEvent(PlayerJoinListener.java:20) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 14 more
    [12:31:25] [Server thread/INFO]: IceFrog[/127.0.0.1:62922] logged in with entity id 292 at ([world]106.22947191552016, 76.5, 240.0609109871841)
    [12:31:30] [Server thread/INFO]: IceFrog lost connection: Disconnected
    
    Code:
    package me.caedus.Events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import me.caedus.ColonyWars.CWPlayer;
    import me.caedus.GameManager.GameManager;
    
    public class PlayerJoinListener implements Listener {
      
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e){
            Player p = (Player) e.getPlayer();
            CWPlayer cwp = new CWPlayer(p);
            if(p.getName().equals("IceFrog")){
              
                if(GameManager.getInstance().getPlayerToCWPlayer() == null)
                    Bukkit.broadcastMessage("breh1");
              
                if(GameManager.getInstance().getBlackTeam() == null)
                    Bukkit.broadcastMessage("black");
              
                if(GameManager.getInstance() == null)
                    Bukkit.broadcastMessage("breh");
              
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getBlueTeam());
                Bukkit.broadcastMessage(""+cwp.getTeam().getColour()+" "+cwp.getTeam().getTeamBalance());
              
            } else {
                GameManager.getInstance().getPlayerToCWPlayer().put(p, cwp);
                cwp.setTeam(GameManager.getInstance().getRedTeam());
            }
            GameManager gm = GameManager.getInstance();
            Bukkit.broadcastMessage("Team: "+ GameManager.getInstance().getPlayerToCWPlayer().get(p).getTeam().getColour());
            CWPlayer cwpp = gm.getPlayerToCWPlayer().get(p);
            Bukkit.broadcastMessage("name test: "+cwpp.getPlayer().getName()+" "+cwpp.getBalance());
            Bukkit.broadcastMessage("Team Balance: "+ gm.getPlayerToCWPlayer().get(p).getTeam().getTeamBalance());
        }
    }
    
    If I comment out the hashmap and team nullcheck, it will print "breh" before continuing further down the code and erroring there.
     
  17. Offline

    ShaneCraftDev

    @Caedus Did you read my answer? In your GameManager class, the 'instance' field is never initialized, only declared as type of GameManager. Your GameManger#getInstance will always return null if you do not initialize the object you are returning. If you still don't understand what is happening or why you are getting a NPE, you shouldn't create plugins but learn basic java.
     
  18. Offline

    Caedus

    @ShaneCraftDev I sort of breezed over it but a lot of it was people discussing other concepts. Either way, I believe i've fixed it, assuming the correct code I needed to add was:

    Code:
    private static GameManager instance = new GameManager();
     
  19. Offline

    I Al Istannen

    @Caedus
    Make that final as it probably should never change and adjust the naming acordingly.
     
  20. Offline

    Caedus

    @I Al Istannen Good point on the final, not sure what you mean about adjusting the name?
     
  21. Offline

    AlvinB

    @Caedus
    He justs means that a static final variable should be all uppercase letters.
    Code:java
    1. private static final GameManager INSTANCE = new GameManager();
     
    I Al Istannen likes this.
  22. Offline

    Caedus

Thread Status:
Not open for further replies.

Share This Page