translateAlternateColorCodes Problem[NPE]

Discussion in 'Plugin Development' started by kaif21, Apr 11, 2016.

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

    kaif21

    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("guild") && sender instanceof Player) {
    3.  
    4. if (!(args.length >= 2)) return false;
    5. String faction = args[1];
    6. switch (args[0].toLowerCase()) {
    7. case "create":
    8. // /guild create0 faction1
    9. if (isFactionRegistered(faction))
    10. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("nameTaken")));
    11. else if (isPlayerInFaction((Player) sender))
    12. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("alreadyGuild")));
    13. else {
    14. addPlayerToFaction((Player) sender, faction);
    15. getConfig().set("Guilds." + args[1] + ".Owner", sender.getName());
    16. getConfig().set("Guilds." + args[1] + ".Players", sender.getName());
    17. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("onGuildCreate").replaceAll("{name}", args[1])));
    18. //
    19. List<String> players = getConfig().getStringList("Guilds." + args[1] + ".Players");
    20. players.add(sender.getName());
    21. getConfig().set("Guilds." + args[1] + ".Players", players);
    22. saveConfig();
    23.  
    24. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("onGuildCreate2")));
    25. }
    26. break;
    27.  

    Code:yaml
    1.  
    2. [17:25:15 INFO]: kaif04 issued server command: /guild create awe [17:25:15 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'guild' in plugin Guild v1.0 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_80] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_80] at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at java.lang.Thread.run(Unknown Source) [?:1.7.0_80] Caused by: java.lang.NullPointerException at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:324) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at india.kaif04.Main.onCommand(Main.java:82) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] ... 15 more
    3.  

    Code:yaml
    1.  
    2. line 82:
    3. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("alreadyGuild")));
    4.  


    messages.yml file:
    Code:yaml
    1.  
    2. nameTaken: "&2[&a&lGBot&2]&c This guild name is already taken!"
    3. alreadyGuild: "&2[&a&lGBot&2]&c You are already in a guild!"
    4. onGuildCreate: "&2[&a&lGBot&2]&e You created a guild named&a&l {name}&e!"
    5. onGuildCreate2: "&2[&a&lGBot&2]&e You can invite players by doing /guild invite player guild"
    6. guildInvite: "&2[&a&lGBot&2]&e You have invited {name} to the guild!"
    7. guildDisband: "&2[&a&lGBot&2]&e You have disbanded the guild!"
    8. disbandFail: "&2[&a&lGBot&2]&e You cannot disband this guild!"
    9. guildInvited "&2[&a&lGBot&2]&e You have been invited to the guild"
    10.  
    11.  



    All the details given above , any help much appreciated !!!
    -kaif21
    if u are finding the error hard to read pastebin link - http://pastebin.com/V0q5fC7u
     
  2. Offline

    kaif21

    @bwfcwalshy
    what do u mean by null..umm
    Code:java
    1.  
    2. public void onEnable() {
    3. plugin = this;
    4. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    5. File f = new File(getDataFolder()+File.separator+"messages.yml");
    6. if (!f.exists()) {
    7. try {
    8. f.createNewFile();
    9. } catch (IOException e) {
    10. e.printStackTrace();
    11. }
    12. }
    13. FileConfiguration config = YamlConfiguration.loadConfiguration(f);
    14. for (String message : config.getConfigurationSection("").getKeys(false)) {
    15. messageData.put(message, config.getString(message));
    16. }
    17.  
    18.  

    My on enable
    cud u please elaborate :( ?
     
  3. @kaif21 Please post the full class.

    Debug the message in that for, print out what message is.
     
  4. @kaif21 Learn Java before trying the Bukkit API. If you don't know what null is then you aren't ready for something like plugins.
     
    Lordloss and Gonmarte like this.
  5. Offline

    kaif21

    Code:java
    1.  
    2. package india.kaif04;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map.Entry;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. public static HashMap<String, Long> invitedPlayers = new HashMap<String, Long>();
    23. public static Main plugin;
    24. public static HashMap<String, String> messageData = new HashMap<String, String>();
    25.  
    26.  
    27. public void onEnable() {
    28. plugin = this;
    29. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    30. File f = new File(getDataFolder()+File.separator+"messages.yml");
    31. if (!f.exists()) {
    32. try {
    33. f.createNewFile();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. FileConfiguration config = YamlConfiguration.loadConfiguration(f);
    39. for (String message : config.getConfigurationSection("").getKeys(false)) {
    40. messageData.put(message, config.getString(message));
    41. }
    42.  
    43. for (String str : getConfig().getKeys(true)) {
    44. String p = getConfig().getString(str);
    45. player_factions.put(str, p);
    46. }
    47. saveConfig();
    48. }
    49.  
    50. public void onDisable() {
    51. for (Entry<String, String> faction : player_factions.entrySet()) {
    52. getConfig().set(faction.getKey(), faction.getValue());
    53. }
    54. saveConfig();
    55. plugin = null;
    56.  
    57. }
    58.  
    59. @Override
    60. public boolean onCommand(final CommandSender sender, Command cmd,
    61.  
    62. String label, String[] args) {
    63.  
    64. // final Player player = (Player) sender;
    65. if (cmd.getName().equalsIgnoreCase("guild") && sender instanceof Player) {
    66.  
    67. if (!(args.length >= 2)) return false;
    68. String faction = args[1];
    69. switch (args[0].toLowerCase()) {
    70. case "create":
    71. // /guild create0 faction1
    72. if (isFactionRegistered(faction))
    73. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("nameTaken")));
    74. else if (isPlayerInFaction((Player) sender))
    75. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("alreadyGuild")));
    76. else {
    77. addPlayerToFaction((Player) sender, faction);
    78. getConfig().set("Guilds." + args[1] + ".Owner", sender.getName());
    79. getConfig().set("Guilds." + args[1] + ".Players", sender.getName());
    80. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("onGuildCreate").replaceAll("{name}", args[1])));
    81. //
    82. List<String> players = getConfig().getStringList("Guilds." + args[1] + ".Players");
    83. players.add(sender.getName());
    84. getConfig().set("Guilds." + args[1] + ".Players", players);
    85. saveConfig();
    86.  
    87. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("onGuildCreate2")));
    88. }
    89. break;
    90. case "invite":
    91. // guild invite0 kaif041 faction2
    92. Player target = Bukkit.getPlayer(args[1]);
    93. invitePlayer(target, 60,args[2]);
    94. sender.sendMessage((ChatColor.translateAlternateColorCodes('&', messageData.get("guildInvite").replace("{name}", target.getName()))));
    95.  
    96. break;
    97. case "disband":
    98. // /guild disband faction
    99. if (getConfig().get("Guilds." + args[1] + ".Owner").equals(sender.getName())) {
    100. getConfig().set("Guilds." + args[1], null);
    101. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("guildDisband")));
    102. } else {
    103. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("disbandFail")));
    104. }
    105. break;
    106. case"list":
    107.  
    108.  
    109. sender.sendMessage("Players:");
    110. List<String> warps = plugin.getConfig().getStringList("Guilds." + args[1] + ".Players");
    111. sender.sendMessage("***Player List***\n" + warps.toString());
    112. sender.sendMessage("Level = 1");
    113.  
    114.  
    115.  
    116.  
    117. break;
    118. }
    119.  
    120.  
    121. } else if (cmd.getName().equalsIgnoreCase("accept") && has((Player) sender, "System.guild")
    122. && sender instanceof Player) {
    123. sender.sendMessage("1");
    124. if (invitedPlayers.containsKey(sender.getName())) {
    125. // If the player was invited at some
    126. // point, check if the invitation
    127. // has
    128. // expired
    129. long inviteEnds = invitedPlayers.get(sender.getName());
    130.  
    131. if (inviteEnds >= System.currentTimeMillis()) {
    132. Player player = (Player) sender;
    133.  
    134.  
    135. List<String> list = getConfig().getStringList("Guilds." + args[0] + ".Players");
    136. if (list.contains(player.getName())) {
    137. sender.sendMessage("You cannot join the guild " + args[0]);
    138. return false;
    139.  
    140. } else
    141. list.add(player.getName());
    142. getConfig().set("Guilds." + args[0] + ".Players", list);
    143. saveConfig();
    144. sender.sendMessage("You have joined the guild " + args[0]);
    145.  
    146. } else { // If the invitation has expired, tell the player
    147. // and
    148. // remove him from the invitation list
    149. invitedPlayers.remove(sender.getName());
    150. sender.sendMessage(ChatColor.RED + "You're invitation has expired!");
    151. sender.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the guild.");
    152. }
    153. } else { // If the player hasn't ever received an invite or the
    154. // last
    155. // one expired and was removed, tell him
    156. sender.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a guild!");
    157. }
    158.  
    159. }
    160.  
    161.  
    162. return false;
    163.  
    164. }
    165.  
    166. public void invitePlayer(Player player, int seconds,String guildName) { // Invite the playe
    167. // for a specified
    168. // time in seconds
    169. if (seconds > 0) {
    170. invitedPlayers.put(player.getName(), ((seconds * 1000) + System.currentTimeMillis()));
    171. player.sendMessage(ChatColor.translateAlternateColorCodes('&', messageData.get("guildInvited"))); // You
    172. // can
    173. // change
    174. // this
    175. // as
    176. // need
    177. // be
    178. player.sendMessage(
    179. ChatColor.GRAY + "" + seconds + " seconds left to type /accept "+ guildName +"and join the clan."); // Same
    180. // with
    181. // this
    182. // message
    183. }
    184. }
    185.  
    186. public boolean has(Player player, String permission) {
    187. return player.hasPermission(permission);
    188. }
    189.  
    190. public static String getGuild(Player player) {
    191. String name = player.getName();
    192. String guild = plugin.getConfig().getString(player.getName());
    193. return guild;
    194.  
    195.  
    196. }
    197.  
    198. public boolean isOwner(Player p, String guild) {
    199. if (getConfig().get("Guilds." + guild + ".Owner").equals(p.getName())) {
    200. return true;
    201. } else
    202. return false;
    203.  
    204. }
    205.  
    206. private void setMessage(String name, String message) {
    207. File f = new File(getDataFolder()+File.separator+"messages.yml");
    208. FileConfiguration config = YamlConfiguration.loadConfiguration(f);
    209. if (!config.isSet(name)) {
    210. config.set(name, message);
    211. try {
    212. config.save(f);
    213. } catch (IOException e) {
    214. e.printStackTrace();
    215. }
    216. }
    217. }
    218. }
    219.  
    220.  

    @bwfcwalshy
    There you go.

    @CodePlaysMinecraft
    Well i know java but since im using this 'Resource ' i found i dont know what null means , hence i have put my full code above
    And yea try not being harsh.
    if i didn't know java i wouldn't be here with a complex problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. Offline

    Gonmarte

    How can you know java without knowing what is the meaning of null?


    Your problem is a bukkit problem, not java, so you cant say that you learn java because you have a bukkit problem...
     
  7. @kaif21 null means the same thing always in Java, whatever "Resource" you use null is still the same.
    Also, please do not static abuse.

    Why is sender final?

    Check if the messageData.get("alreadyGuild") is null.
     
  8. Offline

    mcdorli

    A nullpointer isn't a complex problem, and the others are right, if you don't know what a null is, then you probably don't know java. This is also visible from your code.

    Print out every variable you use in that row, and what you get from them.
     
  9. Online

    timtower Administrator Administrator Moderator

    Cleaned up thread.
    Stay on topic.
     
    0566 likes this.
  10. Offline

    Zombie_Striker

    @kaif21
    As has been said before, messageData is most likely null. Set it equal to something to fix your problem.
     
  11. Offline

    kaif21

    @Zombie_Striker ok ill put like messageData.set(x,y) on onEnable so it isnt null....shudnt that fix it?
     
  12. Offline

    mythbusterma

    @kaif21

    No, you don't "know Java" (whatever that's supposed to mean, anyway), and this is certainly isn't a complex problem. You have to be reasonably familiar with Java before writing a plugin, and we have no way of knowing what the error is because the stack trace you posted and the code you posted don't match. Also, your code is such a mess we probably couldn't even tell you what it was even if they matched. Organise your code and try again.
     
  13. Offline

    Zombie_Striker

    @kaif21
    No, that would not fix the problem, as you would still be referencing a null object (I.e referencing messageData before it is even set top anything). Create the instance of messageData first, and then reference it.
     
Thread Status:
Not open for further replies.

Share This Page