NPE (error) with scoreboard api

Discussion in 'Plugin Development' started by letmedl, Apr 16, 2014.

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

    letmedl

    IV been working on this for about 3 hours straight and if i dont get this done by morning we are going to recode the entire plugin.None of us can figure this out.I am getting a null pointer exception on my plugin when using scoreboard api.Please take a look.The problem is (I think) that bukkit doesent load up before the on enable so the code in before bukkit even loads for the plugin. I was able to fix this using:

    Code:
    public class Main extends JavaPlugin
    implements Listener
    {
    public final HashMap<Player,ArrayList<Block>> playersinlobby = new HashMap<Player,ArrayList<Block>>();
    private ScoreboardManager sbManager = null;
    private Scoreboard sBoard = null;
    private Team red = null;
     
    private Team blue = null;
    public void onEnable()
     
    {
    sbManager = Bukkit.getScoreboardManager();
     
    sBoard = sbManager.getNewScoreboard();
    red = sBoard.registerNewTeam("red");
     
    blue = sBoard.registerNewTeam("blue");
    [COLOR=#800000]But i cant get this to work because you cant set an int to null[/COLOR]
    implements Listener
    {
    int reds = null;
     
    int blues = null;
    public void onEnable()
     
    {
    blues = blue.getsize();
    reds = reds.getsize();
    So i need another way to do this, get around this.
    I have tried this

    Code:
    public void onEnable()
    {
    Snip-
    }
    ScoreboardManager sbManager = Bukkit.getScoreboardManager();
    Scoreboard sBoard = sbManager.getNewScoreboard();
    final Team red = sBoard.registerNewTeam("red");
    final Team blue = sBoard.registerNewTeam("blue");
    
    But it still gives me the error:
    Here is my code to what u need in the plugin:

    Code:
    package me.shooterbret.tpt;
     
    import java.io.PrintStream;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
     
    public class Main extends JavaPlugin
      implements Listener
    {
      public void onEnable()
      {
        System.out.println("[tpt] Enabled Successfully!");
        getServer().getPluginManager().registerEvents(this, this);
        ScoreboardManager sbManager = Bukkit.getScoreboardManager();
        Scoreboard sBoard = sbManager.getNewScoreboard();
        final Team red = sBoard.registerNewTeam("red");
        final Team blue = sBoard.registerNewTeam("blue");
     
      }
     
      public void onDisable()
      {
        System.out.println("[tpt] Disabled Successfully!");
      }
     
      ScoreboardManager sbManager = Bukkit.getScoreboardManager();
      Scoreboard sBoard = sbManager.getNewScoreboard();
      final Team red = sBoard.registerNewTeam("red");
      final Team blue = sBoard.registerNewTeam("blue");
    and here is the error
    [04:04:45 ERROR]: Could not load 'plugins/tpt.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:137) ~[cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:313) ~[cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:236) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:350) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:312) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
    at me.shooterbret.tpt.Main.<init>(Main.java:40) ~[?:?]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_12-ea]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0_12-ea]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0_12-ea]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[?:1.7.0_12-ea]
    at java.lang.Class.newInstance0(Class.java:372) ~[?:1.7.0_12-ea]
    at java.lang.Class.newInstance(Class.java:325) ~[?:1.7.0_12-ea]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[cb.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]

    ... 9 more

    Please i need help asap because have told all the tester to come tomorrow night (expecting this minigame to be done by now) and i , just in general would like to use scoreboard api teams and get to know them because then it just makes things easier.

    Can this be done through the use of multiple classes (never done so before testing now)

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

    Konkz

    Please put the stuff in [ Code ] tags ;-; letmedl
     
  3. Offline

    letmedl

    k

    done Konkz ; )

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

    Konkz

    What is line 40 in class Main?
     
  5. Offline

    letmedl

    Scoreboard sBoard = sbManager.getNewScoreboard();

    Like i said before it is because bukkit is not yet loaded up (I think)

    Konkz ^^

    ok lets try this
    If you right something in another class the plugin must alreadu be laoded right? so cant u from there add the thing in?

    anything u guys, My time is up.

    If nobody knows the solution to this then what about making teams manually, How would u make 2 teams and make them not able to kill eachother.

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

    coasterman10

    You can't call any Bukkit API methods before onEnable() is called. Your instance variables sbManager, sBoard, red, and blue are all being initialized on the plugin's construction. What you should do is leave them uninitialized and then initialize them in the onEnable(). Additionally, you can't set an int to null because it is not an object. You probably mean to set it to zero.

    The way you had it initially would have worked better.
     
  7. Offline

    letmedl

    But in the on enable the rest of the plugin cant access it if u know what i mean. How can we make the manager public so the other voids can use them?

    coasterman10 btw please try to respond as fast as u can we just stopped the recoding of the plugin since u replied and we are waiting for someone to tell us what to do.

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

    coasterman10

    letmedl You can just create an instance variable for your plugin, and then set it in the onEnable(). It doesn't need to be a final variable.
     
  9. Offline

    letmedl

    Ok well i havnt learned this in java yet :p (still learning) so can u give me a quick example, It would help alot. Thanks

    anybody it just doesent only have to be coaster

    anybody... : (

    cmon guys i cant wait at all for this i need an answer or an example tonight cause we are waiting on this and we are in a hurry to move onto the next plugin.

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

    letmedl

    bump....
     
  11. Offline

    TigerHix

    If you can't done a basic plugin that requires very basic programming logic/skills like this, how could you move into harder plugin's development?
     
  12. Offline

    letmedl

    Well instead of stating the obvious it would be nice if you could help.Iv already said that im still learning and sometimes plugins dont come out as simple as i thought they would. Im already almost done with this and if i get an example i will know for all future plugins.

    I just need this last thing to work and thats it, I have completed the plugin and could move on.

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

    coasterman10

    What are you currently stuck on?
     
  14. Offline

    TigerHix


    Nope you are not almost done, and there's dozens of great plugins that is open-sourced on Github.

    Now if you really want us to help, since none of us here can understand your code (yet), please state your objective of writing the plugin and we could write an example for you.

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

    letmedl

    Ok how would you access the scoreboard manager from another class, This is what i have so far.
    Code:
    package ericwang.dodgeball;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
     
    public class ScoreBoardManager {
          ScoreboardManager sbManager = Bukkit.getScoreboardManager();
          Scoreboard sBoard = sbManager.getNewScoreboard();
          final Team red = sBoard.registerNewTeam("red");
          final Team blue = sBoard.registerNewTeam("blue");
          int a;
          int reds = red.getSize();
        int Bothteams = red.getSize() + blue.getSize();
       
        public static void test(){
              ScoreboardManager sbManager = Bukkit.getScoreboardManager();
              Scoreboard sBoard = sbManager.getNewScoreboard();
              final Team red = sBoard.registerNewTeam("red");
              final Team blue = sBoard.registerNewTeam("blue");
              int reds = red.getSize();
              int blues = blue.getSize();
              int Bothteams = red.getSize() + blue.getSize();
        }
        public static int redsize(){
        int reds = -1;
        return reds;
           
        }
    }
    So that is the scoreboardmanager class so now i want to be able to access the integers reds,blues, and bothteams and teams red and blue from another class.So in another class i can something like:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("red")) {
                    red.add(player);
     
  16. Offline

    TigerHix

    letmedl add "public" and "static" modifier.
     
  17. Offline

    1Rogue


    No.

    You would use a getter

    Code:java
    1. public YourObject getYourObject() {
    2. return this.yourObjectInstance;
    3. }


    class fields should only really ever have this combination:
    • private (final is fine)
    • protected (final is fine)
    • package-private final (undeclared)
    • public final
    You shouldn't just willingly expose the class internals, and you certainly shouldn't mark random methods/fields as static just for ease of access.
     
Thread Status:
Not open for further replies.

Share This Page