Solved For loop don't works

Discussion in 'Plugin Development' started by albin900, Jan 12, 2014.

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

    albin900

    Hello i'm making a programming plugin. I trying to make a for loop but when the codes is running i got a error why it not works?
    Code:java
    1.  
    2. while ((cu = in.readLine()) != null){
    3.  
    4. cu = cu.replace("%s",sender.getName());
    5. cu = cu.replace("%pc",tp);
    6. cu = cu.replace("%max",mp);
    7. cu = cu.replace("%ip",ip);
    8. cu = cu.replace("%port",port);
    9. cu = cu.replace("%ver",bukkitversion);
    10. cu = cu.replace("%motd",motd);
    11. cu = cu.replace("%rs",random.getName());
    12.  
    13. System.out.println("AA");
    14.  
    15. for(int i=1; i < 10; i++){
    16.  
    17. System.out.println("V");
    18.  
    19. if(vars[I].isEmpty()){[/I]
    20.  
    21. [I]}else{[/I]
    22. [I]cu = cu.replace("%v_" + i,vars[I]);[/I][/I]
    23. [I]System.out.println(i + " " + vars[I]);[/I][/I]
    24. [I]}[/I]
    25. [I]}[/I]
    26. [I]}[/I]
    27. [I][/I]

    (i don't paste all code)
     
  2. Offline

    AoH_Ruthless

    albin900
    1. We need a stacktrace when you try to run it.
    2. We need the whole class.
     
  3. Offline

    albin900

    Code:java
    1.  
    2. package se.albin900.httptools;
    3.  
    4. import java.io.BufferedReader;
    5. import java.io.IOException;
    6. import java.io.InputStreamReader;
    7. import java.net.URL;
    8. import java.net.URLConnection;
    9. import java.util.Random;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.scoreboard.Objective;
    16. import org.bukkit.scoreboard.Score;
    17. import org.bukkit.scoreboard.Scoreboard;
    18. import org.bukkit.scoreboard.ScoreboardManager;
    19.  
    20. public class Run {
    21. public static void RunUrl(CommandSender sender, String[] args,String url) {
    22. if(sender.hasPermission("httptools.run")){
    23.  
    24.  
    25.  
    26.  
    27. try {
    28. String urltext = url;
    29.  
    30. String tp = "" + Bukkit.getOnlinePlayers().length;
    31. String mp = "" + Bukkit.getMaxPlayers();
    32. String ip = "" + Bukkit.getIp();
    33.  
    34.  
    35. String port = "" + Bukkit.getPort();
    36. String bukkitversion = "" + Bukkit.getBukkitVersion();
    37. String motd = "" + Bukkit.getMotd();
    38.  
    39. Player random = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    40.  
    41. urltext = urltext.replace("%s",sender.getName());
    42. urltext = urltext.replace("%pc",tp);
    43. urltext = urltext.replace("%max",mp);
    44. urltext = urltext.replace("%ip",ip);
    45. urltext = urltext.replace("%port",port);
    46. urltext = urltext.replace("%ver",bukkitversion);
    47. urltext = urltext.replace("%motd",motd);
    48. urltext = urltext.replace("%rs",random.getName());
    49.  
    50. URL ur = new URL(urltext);
    51. URLConnection connection = ur.openConnection();
    52. connection.connect();
    53. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    54.  
    55. String cu = "";
    56.  
    57. String[] vars = new String[100];
    58.  
    59.  
    60.  
    61. while ((cu = in.readLine()) != null){
    62.  
    63. cu = cu.replace("%s",sender.getName());
    64. cu = cu.replace("%pc",tp);
    65. cu = cu.replace("%max",mp);
    66. cu = cu.replace("%ip",ip);
    67. cu = cu.replace("%port",port);
    68. cu = cu.replace("%ver",bukkitversion);
    69. cu = cu.replace("%motd",motd);
    70. cu = cu.replace("%rs",random.getName());
    71.  
    72. System.out.println("AA");
    73.  
    74.  
    75.  
    76. String[] c = cu.split(" ");
    77.  
    78. String g = c[1];
    79.  
    80. String ca = cu.replace("broadcast: ", "");
    81. ca = ca.replace("command: ", "");
    82. ca = ca.replace("scoreboard: ", "");
    83. ca = ca.replace("set ", "");
    84.  
    85.  
    86. if(cu.startsWith("broadcast: ")){
    87.  
    88. if(g.startsWith("<green>")){
    89. g = ca.replace("<green>", "");
    90. Bukkit.broadcastMessage(ChatColor.GREEN + g);
    91. }else if(g.startsWith("<red>")){
    92. g = ca.replace("<red>", "");
    93. Bukkit.broadcastMessage(ChatColor.RED + g);
    94. }else if(g.startsWith("<yellow>")){
    95. g = ca.replace("<yellow>", "");
    96. Bukkit.broadcastMessage(ChatColor.YELLOW + g);
    97. }else if(g.startsWith("<gray>")){
    98. g = ca.replace("<gray>", "");
    99. Bukkit.broadcastMessage(ChatColor.GRAY + g);
    100. }else if(g.startsWith("<aqua>")){
    101. g = ca.replace("<aqua>", "");
    102. Bukkit.broadcastMessage(ChatColor.AQUA + g);
    103. }else if(g.startsWith("<blue>")){
    104. g = ca.replace("<blue>", "");
    105. Bukkit.broadcastMessage(ChatColor.BLUE + g);
    106. }else if(g.startsWith("<black>")){
    107. g = ca.replace("<black>", "");
    108. Bukkit.broadcastMessage(ChatColor.BLACK + g);
    109. }else if(g.startsWith("<white>")){
    110. g = ca.replace("<white>", "");
    111. Bukkit.broadcastMessage(ChatColor.WHITE + g);
    112. }else if(g.startsWith("<gold>")){
    113. g = ca.replace("<gold>", "");
    114. Bukkit.broadcastMessage(ChatColor.GOLD + g);
    115. }else if(g.startsWith("<underline>")){
    116. g = ca.replace("<underline>", "");
    117. Bukkit.broadcastMessage(ChatColor.UNDERLINE + g);
    118. }else{
    119. Bukkit.broadcastMessage(ca);
    120. }
    121. }else if(cu.startsWith("command: ")){
    122.  
    123. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ca);
    124.  
    125.  
    126. }else if(cu.startsWith("set")){
    127.  
    128. String ints = c[1];
    129.  
    130. int id = Integer.parseInt(ints);
    131.  
    132. vars[id] = c[2];
    133.  
    134.  
    135.  
    136. }else if(cu.startsWith("scoreboard: ")){
    137.  
    138. ScoreboardManager manager = Bukkit.getScoreboardManager();
    139. Scoreboard board = manager.getNewScoreboard();
    140.  
    141. if(c[1].startsWith("get")){
    142.  
    143. Objective objective = board.getObjective(c[2]);
    144.  
    145. Score score = objective.getScore(Bukkit.getPlayer(c[3]));
    146.  
    147.  
    148.  
    149.  
    150. }
    151. }
    152.  
    153. }
    154.  
    155.  
    156.  
    157. sender.sendMessage(ChatColor.GREEN + "Executed url! ");
    158. sender.sendMessage(ChatColor.GRAY + "Return data: " + cu);
    159.  
    160.  
    161. } catch (IOException e) {
    162. e.printStackTrace();
    163. }
    164. }
    165. }
    166. }
    167.  
    168.  

    org.bukkit.command.CommandException: Unhandled exception executing command 'ht' in plugin HttpTools v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    Caused by: java.lang.NullPointerException
    at se.albin900.httptools.Run.RunUrl(Run.java:77) ~[?:?]
    at se.albin900.httptools.Httptools.onCommand(Httptools.java:126) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    ... 13 more

    Please don't steal my code :/
     
  4. Offline

    AoH_Ruthless

    albin900
    That's not what I'm here for. I can vouch that most people on this forum have no intention of taking your code and just want to volunteer their time to help you.

    On-Topic:
    Caused by: java.lang.NullPointerException
    at se.albin900.httptools.Run.RunUrl(Run.java:77) ~[?:?]
    at se.albin900.httptools.Httptools.onCommand(Httptools.java:126) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]

    This means you're getting an NPE (NullPointerException) at Run.java, specifically line 77, and at httptools.java (line 126). For the run.java, I think string 'g' is null.
     
  5. Offline

    Superckl1

    As far as I know, this community supports being open source. No one will "steal" your code.

    I think your line numbers are off on the code you provided. Did you remove something before posting it here? There is no way this part:

    Code:java
    1. String[] c = cu.split(" ");
    2.  
    3. String g = c[1];


    could throw a NPE, and that's what the error seems to be pointing to.
     
  6. Offline

    Techy4198

    yeah, line 77 in the code you posted is blank :/
     
  7. Offline

    Gater12

  8. Offline

    albin900

    I think i forgot some lines :/

    Code:java
    1.  
    2. package se.albin900.httptools;
    3.  
    4. import java.io.BufferedReader;
    5. import java.io.IOException;
    6. import java.io.InputStreamReader;
    7. import java.net.URL;
    8. import java.net.URLConnection;
    9. import java.util.Random;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.scoreboard.Objective;
    16. import org.bukkit.scoreboard.Score;
    17. import org.bukkit.scoreboard.Scoreboard;
    18. import org.bukkit.scoreboard.ScoreboardManager;
    19.  
    20. public class Run {
    21. public static void RunUrl(CommandSender sender, String[] args,String url) {
    22. if(sender.hasPermission("httptools.run")){
    23.  
    24. try {
    25. String urltext = url;
    26.  
    27. String tp = "" + Bukkit.getOnlinePlayers().length;
    28. String mp = "" + Bukkit.getMaxPlayers();
    29. String ip = "" + Bukkit.getIp();
    30.  
    31.  
    32. String port = "" + Bukkit.getPort();
    33. String bukkitversion = "" + Bukkit.getBukkitVersion();
    34. String motd = "" + Bukkit.getMotd();
    35.  
    36. Player random = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    37.  
    38. urltext = urltext.replace("%s",sender.getName());
    39. urltext = urltext.replace("%pc",tp);
    40. urltext = urltext.replace("%max",mp);
    41. urltext = urltext.replace("%ip",ip);
    42. urltext = urltext.replace("%port",port);
    43. urltext = urltext.replace("%ver",bukkitversion);
    44. urltext = urltext.replace("%motd",motd);
    45. urltext = urltext.replace("%rs",random.getName());
    46.  
    47. URL ur = new URL(urltext);
    48. URLConnection connection = ur.openConnection();
    49. connection.connect();
    50. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    51.  
    52. String cu = "";
    53.  
    54. String[] vars = new String[100];
    55. String rep = "";
    56. String trep = "";
    57.  
    58.  
    59.  
    60. while ((cu = in.readLine()) != null){
    61.  
    62. cu = cu.replace("%s",sender.getName());
    63. cu = cu.replace("%pc",tp);
    64. cu = cu.replace("%max",mp);
    65. cu = cu.replace("%ip",ip);
    66. cu = cu.replace("%port",port);
    67. cu = cu.replace("%ver",bukkitversion);
    68. cu = cu.replace("%motd",motd);
    69. cu = cu.replace("%rs",random.getName());
    70.  
    71. System.out.println("AA");
    72.  
    73. for(int i=0; i < 10; i++){
    74. rep = "%v_" + i;
    75. trep = vars[i];
    76.  
    77. cu = cu.replace(rep,trep);
    78. }
    79.  
    80. String[] c = cu.split(" ");
    81.  
    82. String g = c[1];
    83.  
    84. String ca = cu.replace("broadcast: ", "");
    85. ca = ca.replace("command: ", "");
    86. ca = ca.replace("scoreboard: ", "");
    87. ca = ca.replace("set ", "");
    88.  
    89.  
    90. if(cu.startsWith("broadcast: ")){
    91.  
    92. if(g.startsWith("<green>")){
    93. g = ca.replace("<green>", "");
    94. Bukkit.broadcastMessage(ChatColor.GREEN + g);
    95. }else if(g.startsWith("<red>")){
    96. g = ca.replace("<red>", "");
    97. Bukkit.broadcastMessage(ChatColor.RED + g);
    98. }else if(g.startsWith("<yellow>")){
    99. g = ca.replace("<yellow>", "");
    100. Bukkit.broadcastMessage(ChatColor.YELLOW + g);
    101. }else if(g.startsWith("<gray>")){
    102. g = ca.replace("<gray>", "");
    103. Bukkit.broadcastMessage(ChatColor.GRAY + g);
    104. }else if(g.startsWith("<aqua>")){
    105. g = ca.replace("<aqua>", "");
    106. Bukkit.broadcastMessage(ChatColor.AQUA + g);
    107. }else if(g.startsWith("<blue>")){
    108. g = ca.replace("<blue>", "");
    109. Bukkit.broadcastMessage(ChatColor.BLUE + g);
    110. }else if(g.startsWith("<black>")){
    111. g = ca.replace("<black>", "");
    112. Bukkit.broadcastMessage(ChatColor.BLACK + g);
    113. }else if(g.startsWith("<white>")){
    114. g = ca.replace("<white>", "");
    115. Bukkit.broadcastMessage(ChatColor.WHITE + g);
    116. }else if(g.startsWith("<gold>")){
    117. g = ca.replace("<gold>", "");
    118. Bukkit.broadcastMessage(ChatColor.GOLD + g);
    119. }else if(g.startsWith("<underline>")){
    120. g = ca.replace("<underline>", "");
    121. Bukkit.broadcastMessage(ChatColor.UNDERLINE + g);
    122. }else{
    123. Bukkit.broadcastMessage(ca);
    124. }
    125. }else if(cu.startsWith("command: ")){
    126.  
    127. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ca);
    128.  
    129.  
    130. }else if(cu.startsWith("set")){
    131.  
    132. String ints = c[1];
    133.  
    134. int id = Integer.parseInt(ints);
    135.  
    136. if(c[2].startsWith("getscore ")){ //set 0 INT 1 getscore 2 objective 3 player 4
    137.  
    138. Player player = Bukkit.getPlayer(c[4]);
    139.  
    140. ScoreboardManager manager = Bukkit.getScoreboardManager();
    141. Scoreboard board = manager.getNewScoreboard();
    142. Objective objective = board.getObjective(c[3]);
    143. Score score = objective.getScore(player);
    144.  
    145.  
    146.  
    147. }else{
    148.  
    149. vars[id] = c[2];
    150.  
    151. }
    152.  
    153.  
    154.  
    155. }else if(cu.startsWith("scoreboard: ")){
    156.  
    157. if(c[1].startsWith("set")){
    158.  
    159. }
    160. }
    161.  
    162. }
    163.  
    164.  
    165.  
    166. sender.sendMessage(ChatColor.GREEN + "Executed url! ");
    167. sender.sendMessage(ChatColor.GRAY + "Return data: " + cu);
    168.  
    169.  
    170. } catch (IOException e) {
    171. e.printStackTrace();
    172. }
    173. }
    174. }
    175. }
    176.  
    177. [/i]

    Here is the full code i made some edits since first post but still same error :/


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

    WhatAaCow

    albin900 line 80 "String[] c = cu.split(" ");" can do a NPE
     
  10. Offline

    Superckl1

    WhatAaCow

    How? He uses the cu variable multiple times before that, and I don't see a way it could be set to null right before line 80.

    albin900

    The error is indeed on line 77. You do this:

    Code:java
    1. trep = vars[I];[/I]
    2.  
    3. [I]cu = cu.replace(rep,trep);[/I]


    vars is an empty array with 100 slots, meaning you're setting trep to null.

    EDIT: the syntax is getting wierd because of the i, but you see what I'm saying :p
     
  11. Offline

    WhatAaCow

    Superckl1 if you split SPACE!! (" "), and there is no space, then it returns null
     
  12. Offline

    Superckl1

    WhatAaCow

    No... I ran this code:

    Code:java
    1. System.out.println(Arrays.toString("test".split(" ")));


    and got this: [test]

    Definitely not null.
     
Thread Status:
Not open for further replies.

Share This Page