I am trying to read from a file and add each line from the file to my array “Titles” but keep getting the error:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
Any Ideas? I am getting the error because of the line reading:
Titles[lineNum] = m_line;
My code:
String[] Titles={};
int lineNum;
m_fileReader = new BufferedReader(new FileReader("random_nouns.txt"));
m_line = m_fileReader.readLine();
while (m_line != null)
{
Titles[lineNum] = m_line;
m_line = m_fileReader.readLine();
lineNum++;
}
Thank you in advance!
array indexes start with 0. if length of an array is N, then the last index would be N-1. currently your array has a length of zero and you are trying to access an element at index 0(which does exist).
and in your while loop
I would suggest you use ArrayList instead of Array in your case. as ArrayList is dynamic (you dont have to give size for it)