Code not working, need help!

Discussion in 'Plugin Development' started by Xinito, Oct 16, 2013.

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

    Xinito

    Hello,

    When i'm reloading my server the plugin gives this error in the console. When I enable the server there no errors and everything works fine. What goes wrong?

    Code:
    2013-10-16 18:48:32 [WARNING] [Project] Task #35 for Project v0.1 generated an exception
    java.lang.IllegalArgumentException: Scoreboard cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer.setScoreboard(CraftPlayer.java:1005)
        at me.mistachocolata.project.Main.update(Main.java:109)
        at me.mistachocolata.project.Main$1.run(Main.java:47)
        at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:58)
        at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:520)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    My code:

    Code:java
    1. package me.mistachocolata.project;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5.  
    6. import net.milkbowl.vault.economy.Economy;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.event.player.PlayerQuitEvent;
    16. import org.bukkit.plugin.RegisteredServiceProvider;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.scoreboard.DisplaySlot;
    19. import org.bukkit.scoreboard.Objective;
    20. import org.bukkit.scoreboard.Score;
    21. import org.bukkit.scoreboard.Scoreboard;
    22. import org.bukkit.scoreboard.ScoreboardManager;
    23.  
    24. public class Main extends JavaPlugin implements Listener {
    25. public static Main mainPlugin = null;
    26. public PlayerDataYAML yaml = new PlayerDataYAML();
    27. public Logger logger;
    28. public static Economy economy = null;
    29.  
    30. public static HashMap<String, Scoreboard> playerScoreboards = new HashMap<String, Scoreboard>();
    31. public static HashMap<String, Score> ratingScore = new HashMap<String, Score>();
    32.  
    33. public void onEnable() {
    34. mainPlugin = this;
    35.  
    36. this.logger = this.getLogger();
    37. this.logger.info("Plugin has been enabled.");
    38.  
    39. Bukkit.getPluginManager().registerEvents(this, this);
    40. Bukkit.getPluginManager().registerEvents(new PlayerDataYAML(), this);
    41. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    42.  
    43. @Override
    44. public void run() {
    45. for (Player player:Bukkit.getOnlinePlayers()) {
    46. if (!playerScoreboards.containsKey(player.getName())) {
    47. update (player);
    48. set (player);
    49. }
    50. }
    51. }
    52. }, 0L, 50L);
    53. }
    54.  
    55. public void onDisable() {
    56. this.logger.info("Plugin has been disabled.");
    57.  
    58. for (Player p:Bukkit.getOnlinePlayers()) {
    59. Score scoreRating = (Score) ratingScore.get(p.getName().toLowerCase());
    60. int intRating = scoreRating.getScore();
    61. PlayerDataYAML.set(p.getName(), "Data.rating", String.valueOf(intRating));
    62.  
    63. }
    64.  
    65. }
    66.  
    67. @EventHandler
    68. public void onPlayerJoin(PlayerJoinEvent event) {
    69. Player player = event.getPlayer();
    70. String pname = player.getName().toLowerCase();
    71.  
    72. if (!playerScoreboards.containsKey(pname)) {
    73. create(player, pname, "ServerName");
    74. set(player);
    75. }
    76. }
    77.  
    78. @EventHandler
    79. public static void CreateOnLeave(PlayerQuitEvent ev) {
    80. Player player = ev.getPlayer();
    81. Score scoreRating = (Score) ratingScore.get(player.getName().toLowerCase());
    82. int intRating = scoreRating.getScore();
    83. PlayerDataYAML.set(player.getName(), "Data.rating", String.valueOf(intRating));
    84. }
    85.  
    86. public static void create(Player player, String BoardName, String BoardDisplayName) {
    87. String pname = player.getName().toLowerCase();
    88.  
    89. ScoreboardManager manager = Bukkit.getScoreboardManager();
    90. Scoreboard board = manager.getNewScoreboard();
    91.  
    92. Objective objective = board.registerNewObjective(BoardName, "dummy");
    93.  
    94. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    95. objective.setDisplayName(BoardDisplayName);
    96.  
    97. //Score rating = objective.getScore(Bukkit.getOfflinePlayer(mainPlugin.getConfig().getString("name").replaceAll("(&([a-f0-9]))", "\u00A7$2")));
    98. Score rating = objective.getScore(Bukkit.getOfflinePlayer("Rating:"));
    99.  
    100. rating.setScore(0);
    101.  
    102. ratingScore.put(pname, rating);
    103.  
    104. playerScoreboards.put(pname, board);
    105. }
    106.  
    107. public static void update(Player player) {
    108. String pname = player.getName().toLowerCase();
    109. player.setScoreboard(playerScoreboards.get(pname));
    110.  
    111. Score rating = ratingScore.get(pname);
    112.  
    113. String strRating = (String) PlayerDataYAML.get(pname, "Data.rating");
    114. int intRating = Integer.valueOf(strRating);
    115. rating.setScore(intRating);
    116. }
    117.  
    118. public static void set(Player player) {
    119. String pname = player.getName().toLowerCase();
    120. player.setScoreboard(playerScoreboards.get(pname));
    121. }
    122.  
    123. public static void addScore(Player player, String ScoreType, int Score) {
    124. String pname = player.getName().toLowerCase();
    125.  
    126. if (ScoreType.equals("rating")) {
    127. Score rating = ratingScore.get(pname);
    128. rating.setScore(rating.getScore() + Score);
    129. }
    130. }
    131.  
    132. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    133. if (label.equalsIgnoreCase("rating")) {
    134. Player player = (Player) sender;
    135.  
    136. String pname = player.getName().toLowerCase();
    137. player.setScoreboard(playerScoreboards.get(pname));
    138. PlayerDataYAML.set(player.getName(), "Data.rating", String.valueOf(Integer.valueOf((String)
    139. PlayerDataYAML.get(player.getName(), "Data.rating")) + 1));
    140. }
    141. else if (label.equalsIgnoreCase("item")) {
    142. Player player = (Player) sender;
    143. Economy econ = null;
    144. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    145.  
    146. if (economyProvider != null) {
    147. econ = economyProvider.getProvider();
    148. }
    149.  
    150. econ.depositPlayer(player.getName(), 5);
    151. player.getInventory().addItem(Items.leatherHelmetProt2);
    152. }
    153. return false;
    154. }
    155. }
    156.  
     
  2. Offline

    Harry5573

    java.lang.IllegalArgumentException: Scoreboard cannot be null
    Scoreboards null.. Did you create it?
     
  3. Offline

    Chinwe

    map.get() returns null if the map doesn't contain that key, so you should check that playerScoreboards.contains(pname) before setting his scoreboard to it.
     
  4. Offline

    Xinito

    Code:java
    1. public static void set(Player player) {
    2. if (!playerScoreboards.containsKey(player.getName())) {
    3. String pname = player.getName().toLowerCase();
    4. player.setScoreboard(playerScoreboards.get(pname));
    5. }
    6. }
    7.  
    8. public static void addScore(Player player, String ScoreType, int Score) {
    9. String pname = player.getName().toLowerCase();
    10.  
    11. if (ScoreType.equals("rating")) {
    12. Score rating = ratingScore.get(pname);
    13. rating.setScore(rating.getScore() + Score);
    14. }
    15. }


    Didnt make the fix, still the same error.
     
  5. Offline

    Chinwe

    The other way round :0 You want to set his scoreboard if the map does contain his name :confused:
     
  6. Offline

    1Rogue


    or get the value and do a null check, which would be more memory efficient.
    Code:java
    1. Object o = Map.get("key");
    2. if (o != null) {
    3. // continue ...
    4. }


    Edit: This also helps in future cases with maps as well, because using .containsKey and a null check are two different things. .containsKey will return null if you put a null value into the map, but will still return true for containsKey.
     
  7. Offline

    Xinito

    1Rogue, How would I use this in my code? I dont quite really understand the Map stuff :s
     
  8. Offline

    1Rogue


    get the value, then check if it is null.
     
  9. Offline

    Xinito

    1Rogue Which value? I dont understand the null checker at all. :\
     
  10. Offline

    1Rogue

    Code:java
    1. ObjectOfWhatYouAreGetting variableName = mapYouAreUsing.get("value of the key");
    2. if (variableName != null) {
    3. // do code
    4. } else {
    5. // the map returned null
    6. }
     
  11. Offline

    Xinito

    Can anyone help me by fixing my code? I dont understand about the Map stuff at all, I tried alot of different things nothing works... :\
     
  12. Offline

    5pHiNxX



    this should fix it:

    use
    Code:java
    1. Scoreboard tempScoreboard = playerScoreboards.get(pname);
    2. if (tempScoreboard != null) {
    3. player.setScoreboard(tempScoreboard);
    4. }
    5.  

    instead of
    Code:java
    1. player.setScoreboard(playerScoreboards.get(pname));
    2.  


    everytimes
     
  13. Offline

    Xinito

    @5pHiNxX This worked, thanks!
     
Thread Status:
Not open for further replies.

Share This Page