Solved NullPointer when calling scoreboard between classes

Discussion in 'Plugin Development' started by flash110, Jun 7, 2016.

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

    flash110

    So I am making a multi-class program to help me learn, and it is all going well except for the fact that I am getting this error. Here is the code:
    Main Class (open)

    Code:
    package me.flash110;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    
    public class SAM extends JavaPlugin {
        private static SAM instance;
    
        public SAM() {
            instance = this;
        }
    
        public static SAM getInstance() {
            return instance;
        }
       
        ArrayList<String> players = new ArrayList<>();
        Listeners listeners = new Listeners();
        Teams teams = Teams.getInstance();
        private Scoreboard scoreboard;
       
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(listeners, this);
            teams.initializeTeam("red", ChatColor.RED.toString());
           teams.initializeTeam("blue", ChatColor.BLUE.toString());
           getBoard();
        }
       
        public Scoreboard getBoard() {
            scoreboard = getServer().getScoreboardManager().getMainScoreboard();
            return scoreboard;
        }
    }
    

    Teams Class (open)
    Code:
    package me.flash110;
    
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.Scoreboard;
    
    public class Teams {
       
       
        //Constructor
        private static Teams instance;
    
        public Teams() {
            instance = this;
        }
    
        public static Teams getInstance() {
            return instance;
        }
       
       
        //The rest
        private Scoreboard scoreboard;
        SAM plugin = SAM.getInstance();
       
        public void initializeTeam(String teamName, String prefix) {
            scoreboard = plugin.getBoard();
               if (scoreboard.getTeam(teamName) == null) {
                   scoreboard.registerNewTeam(teamName).setPrefix(prefix);
           }
        }
       
        @SuppressWarnings("deprecation")
        public void addTeam(Player p) {
            if (scoreboard.getTeam("red").getPlayers().size() < scoreboard.getTeam("blue").getPlayers().size()) {
                scoreboard.getTeam("red").addPlayer(p);
            }
            else {
                scoreboard.getTeam("blue").addPlayer(p);
            }
        }
    }
    
    

    NullPointer Error (open)
    Code:
    [21:13:53] [Server thread/INFO]: [SAM] Enabling SAM v1.0
    [21:13:53] [Server thread/ERROR]: Error occurred while enabling SAM v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.flash110.SAM.onEnable(SAM.java:28) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.Bukkit.reload(Bukkit.java:535) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [craftbukkit.jar:git-Spigot-76236cb-9cd1111]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    [21:13:53]
    

    If you find anything that could be causing it then let me know.
    And one more thing, please do not respond with; "Put it all in one class", or "learn java".
    Other than that have a nice day/night! :D
     
  2. Offline

    mythbusterma

    @flash110

    Learning Java is a very good idea though, it would help you diagnose this issue quite quickly. Your issue is that you've implemented the singleton design anti-pattern incorrectly, and Teams.getInstance() is returning null. Look up how to properly make a singleton and you will realise your issue.
     
    flash110 likes this.
  3. Offline

    flash110

    @mythbusterma I only said the "learn Java" thing, because people decide to just type that and be done with the post. I have taken a Java course and have 2 books on Java. I was trying this to figure out the different ways to structure multi-class Java programs. It is late now and I will look into it tomorrow, thanks for the help though! (Sorry if I came across rude, wasn't trying to be) :p
    EDIT:
    I looked into it a little bit just now and found this bit
    Code:
     
    
    public class Class1 {
        private Main instance;
    
        public Class1(Main instance) {
            this.instance = instance;
        }
    
        public void doSomething() {
            instance.doSomethingElse();
        }
    }
    
    I will mess around with this more tomorrow, I edited this so you can see and let me know if this is the right way to do it.
     
    Last edited: Jun 7, 2016
  4. Offline

    mythbusterma

    @flash110

    Usually, when a singleton doesn't need extra data during initialization, it takes this form:

    Code:
    private static ClassName instance;
    
    public static ClassName getInstance() {
        if (instance == null) {
            instance = new ClassName();
        }
        return instance;
    }
    
    This way it is implicitly initialized the first time it is needed.
     
  5. Offline

    flash110

    @mythbusterma Okay, at least I didn't have it completely wrong :p I'll go try that now and edit this comment with what happened. EDIT:
    After fixing up the code with what you suggested the sane error occured, and it happens when calling
    Code:
     teams.initializeTeam("blue", ChatColor.BLUE.toString()); 
    from the Teams class. I think it has to do with the main scoreboard it gets from the main class right before that. I made a test method in the Teams class with a simple bit that just prints something to console and it worked. It just cant call the initializeTeam method... If anyone else has any ideas why, let me know. @mine-care since you have solved one of my problems before, do you have any ideas?
     
    Last edited: Jun 8, 2016
  6. Offline

    mythbusterma

    @flash110

    I can't see your code, I can't help you.
     
  7. Offline

    flash110

    Oops I forgot to update this thread. I got it to work so I'm setting it to solved now.
     
Thread Status:
Not open for further replies.

Share This Page