title: How to avoid getting java.lang.ArrayIndexOutOfBoundsException
body: <p>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:</p>
  
  <pre><code>public void writeBucket(int bucket, String importFile, String[][] allrecords)
  {
  
    theDisk = new FakeDisk();
  
    for(int z = 0; z &lt; bucket; z++)
    {
  
      try
       {
  
        for(int j = 0; j &lt; 7; z++)//for(int j = 0; j &lt; 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());
    }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help is appreciated!</p>