-1

I am trying to write a program that emulates a database by writing to a text file. I can read a text file full of data to use and then convert it to a byte array to store into another text file. The issue I'm having is as I'm converting from a string to a byte array I keep getting the java.lang.ArrayIndexOutOfBoundsException: 8. I hard coded values into my for loops so that it shouldn't be an invalid index for the array but nothing seems to fix the problem. Here is my function that the error shows up in:

public void writeBucket(int bucket, String importFile, String[][] allrecords)
{

  theDisk = new FakeDisk();

  for(int z = 0; z < bucket; z++)
  {

    try
     {

      for(int j = 0; j < 7; z++)//for(int j = 0; j < allrecords[z].length; z++)
      {

        if(allrecords[z][j] == null) //this is the line where the error shows up
        {
          continue;
        }

        theDisk.writeSector(z, allrecords[z][j].getBytes());
      }
     }
     catch(Exception e)
     {
         //System.out.println(e.getMessage());//this prints the number 8 when not commented out
       continue;
     }

  }

  try
  {

    FileWriter fwrite = new FileWriter(importFile);

    fwrite.write("\n\n\n\n");
    fwrite.close();

  }
  catch (Exception e)
  {
    System.err.println("Error: " + e.getMessage());
  }

}

I put the loop in a try/catch thinking that it would still at least output the bytes to my text file and then not add any more to the file once it hits an invalid index, but that's not the case. I'm mainly having trouble figuring out why I keep getting this error. I can print out the array no problem and everything shows up if I don't try to write it to the text file.

Any help is appreciated!

Frank A.
  • 1,987
  • 3
  • 16
  • 19
  • Please print stack trace – zaffargachal Oct 16 '12 at 06:18
  • Which line of your code corresponds to line 8 of the exception you get? – Andreas Fester Oct 16 '12 at 06:19
  • how you are getting bucket value, it seems to be causing exception, can you send the code how you are calling this method,and how you are setting bucket variable value – zaffargachal Oct 16 '12 at 06:21
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at frankaddeliaproject1.FrankAddeliaProject1.writeBucket(FrankAddeliaProject1.java:242) at frankaddeliaproject1.FrankAddeliaProject1.processCommand(FrankAddeliaProject1.java:133) at frankaddeliaproject1.FrankAddeliaProject1.showMenu(FrankAddeliaProject1.java:52) at frankaddeliaproject1.FrankAddeliaProject1.main(FrankAddeliaProject1.java:34) Java Result: 1 – Frank A. Oct 16 '12 at 06:24
  • Also, this is the line that the error shows up at if(allrecords[z][j] == null) – Frank A. Oct 16 '12 at 06:24
  • bucket is actually hard coded to 7 in the loop that calls this function... here is the option in my loop that handles calling this method: http://pastebucket.com/4032 – Frank A. Oct 16 '12 at 06:28

4 Answers4

5

This is the problem:

for(int j = 0; j < 7; z++)

Look at the loop initialization and the loop condition, both of which use j - then look at the increment, which is changing the value of z.

Even the commented out version is still broken, as it's still incrementing z.

In fact, you don't need j at all as far as I can see. You can change your inner loop to:

for (String text : allrecords[z])
{
    if (text == null)
    {
        continue;
    }
    theDisk.writeSector(z, text.getBytes());
}

However, I would strongly encourage you not to call getBytes() without an encoding in the first place. If you really want the default platform encoding, specify that explicitly. If you want some other encoding such as UTF-8, specify that. If you don't know what encoding you want, you need to take a step back and think carefully about your data.

(It's also slightly odd that you're calling writeSector with the same value of z each time... isn't that going to just overwrite the data?)

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Hello, thank you for that info! The writesector method I'm using should write to the simulated sector number (what I have z representing in the loop). Although, I have to admit I'm not 100% sure if that's the case because I didn't write the writeSector method personally. Java isn't my main programming language so I keep running into these types of errors unfortunately. Also, maybe you have some insight as to why when this runs with the loop incrementing fixed that it doesn't write anything to the file? Would that be due to not specifying UTF-8? – Frank A. Oct 16 '12 at 06:47
  • @FrankA.: No, it's impossible to say without more information. We don't know whether `allrecords[z]` has any non-null elements, we don't know what `writeSector` does, etc. You should debug into the code. – Jon Skeet Oct 16 '12 at 07:21
  • Hey thanks again for your help! It turns out the method in the class I'm using doesn't alert you if the file it's attempting to write to is set to null or not. Just my professor testing how observant we are as programmers I'm guessing ;) – Frank A. Oct 16 '12 at 08:07
1

You have the answer, you could use commented loop (well instead of z++, use j++):

for(int j = 0; j < 7; z++)  //for(int j = 0; j < allrecords[z].length; j++)
Nandkumar Tekale
  • 14,991
  • 7
  • 52
  • 85
  • That actually still gives me the same out of bounds error. I commented that out to see if hard coding in the value would fix anything. I still get the same error if I hard code the value as 1 instead of 7 also – Frank A. Oct 16 '12 at 06:22
  • @Nandkumar. Does not solve problem.. OP is incrementing `z` in both the loops. – Rohit Jain Oct 16 '12 at 06:22
  • Doh, thanks so much! I've been looking at the code for too long and stopped noticing that kind of thing – Frank A. Oct 16 '12 at 06:34
  • @FrankA. : Did you check `Jon Skeet's` answer? use it. – Nandkumar Tekale Oct 16 '12 at 06:41
1

In the inner loop, it should be j++, not z++:

for(int j = 0; j < 7; j++)
Pablo
  • 3,635
  • 2
  • 28
  • 44
-1

try this, in this way you will not get the ArrayIndexOutOfBoundException. in case of 2d array, .length will give the length for the first index. and allrecords[z].length will give th length for the second index.

for(int z = 0; z < allrecords.length; z++)


for(int j = 0; j < allrecords[z].length; z++)
rbhawsar
  • 827
  • 7
  • 25