Plugin Works as needed, still throws ArrayIndexOutOfBoundsException

Discussion in 'Plugin Development' started by nateracecar5, Jan 23, 2014.

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

    nateracecar5

    Okay, so I have a plugin that I built for the server owner of a server I play on. It's fairly simple: It logs a string of text that you enter into command in a file. The thing is, even though it logs EXACTLY as it should, I'm getting ArrayIndexOutOfBoundsException when I do the command, and my debug message is popping up in the chat. Any help? Here is the code:
    Code:java
    1. package me.redstonefreak589.SimpleLog;
    2.  
    3. import java.io.File;
    4. import java.io.FileWriter;
    5. import java.io.IOException;
    6. import java.io.PrintWriter;
    7. import java.text.DateFormat;
    8. import java.text.SimpleDateFormat;
    9. import java.util.Calendar;
    10. import java.util.logging.Logger;
    11.  
    12. import org.apache.commons.lang.StringUtils;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.plugin.PluginDescriptionFile;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin{
    21. public final Logger logger = Logger.getLogger("Minecraft");
    22. public static Main plugin;
    23.  
    24. @Override
    25. public void onDisable() {
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. this.logger.info(pdfFile.getName() + " has been disabled!");
    28. }
    29.  
    30. @Override
    31. public void onEnable() {
    32. PluginDescriptionFile pdfFile = this.getDescription();
    33. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled!");
    34. }
    35. public void logToFile(String message,String fileName)
    36.  
    37. {
    38.  
    39. try
    40. {
    41. File dataFolder = getDataFolder();
    42. if(!dataFolder.exists())
    43. {
    44. dataFolder.mkdir();
    45. }
    46.  
    47. File saveTo = new File(getDataFolder(), fileName + ".txt");
    48. if (!saveTo.exists())
    49. {
    50. saveTo.createNewFile();
    51. }
    52.  
    53.  
    54. FileWriter fw = new FileWriter(saveTo, true);
    55.  
    56. PrintWriter pw = new PrintWriter(fw);
    57.  
    58. pw.println(message);
    59.  
    60. pw.flush();
    61.  
    62. pw.close();
    63.  
    64. } catch (IOException e)
    65. {
    66.  
    67. e.printStackTrace();
    68.  
    69. }
    70.  
    71. }
    72.  
    73. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    74. String newString = "";
    75. String oldString = "";
    76. Player player = (Player) sender;
    77. if(commandLabel.equalsIgnoreCase("log")){
    78. if(args.length != 0){
    79. DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    80. Calendar cal = Calendar.getInstance();
    81. String date = dateFormat.format(cal.getTime());
    82. logToFile("=" + date + "=","logs");
    83. for(int i = 0; i <= args.length; i++){
    84. try{
    85. oldString = args[i];
    86. newString = newString + oldString + " ";
    87. } catch (Exception e){
    88. String error = e.toString();
    89. logToFile(error,"errors");
    90. player.sendMessage(ChatColor.RED + "ERROR: A logging error has occured (This is common). Please refer to the errors.txt file for more information");
    91. }
    92.  
    93. }
    94. logToFile(newString,"logs");
    95. logToFile(StringUtils.repeat("=", date.length()/2-3) + "End Of Log" + StringUtils.repeat("=", date.length()/2-3),"logs");
    96. logToFile(" ","logs");
    97. }else{
    98. player.sendMessage(ChatColor.RED + "Usage: /log [text]");
    99. }
    100. }
    101. return false;
    102. }
    103. }
    104. [/i]



    The error is in the for statement inside the try and catch statements (On line 85). The line says "oldString = args;"
    Am I doing something wrong, or am I overthinking it? I don't know much java, but I do know lot's about coding bukkit, so I don't really have much of an understanding of these kinds of things. The only thing I know how to do is find the error type, and the line it was on, then fix it. This one however, is stumping me.


    Also, I have NO idea what those little [/i]'s are. They just appeared there. :\

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

    xTigerRebornx

    nateracecar5
    Code:
    int i = 0; i <= args.length; i++;
    This is the problem, Since you are starting at 0, then going to args.length, it will go 1 over the Array's bounds, since the Array will start at 0, but have a length of 1, you could subtract 1 from the length
    Code:
    int i = 0; i <= (args.length - 1); i++;
     
  3. Offline

    nateracecar5

    Okay I will try that, and btw, thanks for the quick response. :)

    Thank you so much! I knew I was overlooking it, I kept doing
    Code:java
    1. for(int i = 1; i <= (args.length+1); i++){
    2. //do stuff
    3. }

    to try and expirement, but it didn't work.

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

    xTigerRebornx

    nateracecar5 Because you are increasing both by 1, which is the same as starting both from their original. (Either start at one, or use args.length - 1)
     
  5. Offline

    nateracecar5

    Okay, thanks
     
  6. Offline

    Goblom

    nateracecar5 Change this
    Code:
    int i = 0; i <= args.length; i++;
    to
    Code:
    int i = 0; i < args.length; i++; // (remove the '=')
    Doing so would make your OutOfBoundsException stop and it also prevents you from having to add or subract 1 from the the length of the args... (An example is what xTigerRebornx suggested)


    Edit: I used to the same thing with <= when i first started writing java. I have since learned that use <= in a for loop can cause much pain.
     
  7. Offline

    nateracecar5

    Goblom Thanks. I will remember this in the future.
     
Thread Status:
Not open for further replies.

Share This Page