title: How to fix 'ava.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1' error in Java
body: <p>I'm trying to create an instance of the "PhoneEntry" class using data ripped from a .txt file. The .split() seems to effectively create an array (info) that contains the three values from each line in the doc (first name, last name, and phone #), but I get an out of bounds error when I attempt to create an instance of the "PhoneEntry" class using the array[index] formula to call each value. I managed to print all the indexes in the info array, so they contain the correct values, but it's failing to create a new "PhoneEntry" with them. </p>
  
  <p>So far I have...</p>
  
  <p>1) Printed all the values to make sure the filereader loop is doing its job in correctly assembling the "info" array </p>
  
  <p>2) Printed out the lengths of the info arrays. It returned five '3's and one '1'. There should only be 5 arrays because there are only 5 lines with content in phonebook.txt, but maybe there is a secret '/n' or something, if so, how would I remove that character?</p>
  
  <pre><code>PhoneBook()   // constructor for the phone book
      {
          try {
              BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\javaprojects\\phonebook2\\phonebook.txt");
              String line;
              try {
                  while ((line = bufferedReader.readLine()) != null) {
                      String[] info = line.split(" ");
                      System.out.println(info[0] + " " + info[1] + " "+info[2]);
                      phoneBook.add(new PhoneEntry(info[0], info[1], info[2]));
                  }
                  bufferedReader.close();
              } catch (IOException e2){
                  e2.printStackTrace();
              }
          } catch (FileNotFoundException e1){
              e1.printStackTrace();
          }
  
  
      }
  </code></pre>
  
  <p>.txt file:</p>
  
  <pre><code>
  Ali Katt (312)223-1937
  Ima Knob (860)399-3044
  Jo King (913)883-2874
  Stew Pid (651)698-4631
  Ug Lee (815)439-9271
  
  </code></pre>
  
  <p>The exact error message is: </p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
      at PhoneBook.&lt;init&gt;(PhoneBook.java:16)
      at PhoneBookTester.main(PhoneBookTester.java:10)
  </code></pre>
  
  <p>Line 16 refers to the print statement, but all the values actually are printed out correctly, including the last index (2). Before the print statement was there, the error referred to the new phoneEntry.</p>
  
  <p>I need it to actually create an instance of "PhoneEntry" as well as add it to the array list "phoneBook"</p>