Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dalton Coble
6,489 PointsI get errors I don't understand when I run my code.
Errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.teamtreehouse.model.SongBook.importFrom(SongBook.java:38) at Karaoke.main(Karaoke.java:8) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
It would be too much to give all the code here so I have a download link:
4 Answers
Christopher Augg
21,223 PointsDalton,
I finally had time to download your files and test myself.
Your songs.txt file has extra lines in it.
Go to the end of the songs.txt and remove the extra lines. The error is being thrown when it gets to line 16, 17, 18 and it is blank in the text file.
Regards,
Chris
Christopher Augg
21,223 PointsDalton,
java.lang.ArrayIndexOutOfBoundsException: 1 is caused by your code trying to access an array at index 1 when there are less than 2 elements.
at com.teamtreehouse.model.SongBook.importFrom(SongBook.java:38) at Karaoke.main(Karaoke.java:8) tells us that this happens when the songBook.importFrom method is called from within the main method.
Therefore, we trace the method call and find the array that seems to be having the issue within the importFrom method only to find that your code is perfectly fine:
public void importFrom(String fileName) {
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
) {
String line;
while ((line = reader.readLine()) != null) {
String[] args = line.split("\\|");
addSong(new Song(args[0], args[1], args[2])); //args[1] is out of bounds for some reason
}
} catch (IOException ioe) {
System.out.printf("Problems loading %s %n", fileName);
ioe.printStackTrace();
}
}
Since this code is correct, we need to figure out why the array is not the correct size. Considering the size of the array is dependent on the splitting of items with the | separator, it seems logical that there is an issue reading the songs.txt file into the buffered reader and the line.split assignment to args.
You could do some tests to see if this is indeed the case. Place the following line just above the addSong method:
System.out.println("The size of args: " + args.length);
You should get something like:
The size of args: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.teamtreehouse.model.SongBook.importFrom(SongBook.java:39)
at Karaoke.main(Karaoke.java:8)...
The size of the args should be 3 all 15 times if there were no issues.
You might want to get the original songs.txt file and copy over the one you have. Make sure it is in the correct directory location within your project. Complete a clean build of the project.
I hope this helps.
Regards,
Chris
Dalton Coble
6,489 PointsHi i tried that line in you suggested but the code has no idea what args is for some reason. update : i notice that the args is in the while loop above so why isn't is working? that is what we need to know now.
Dalton Coble
6,489 PointsThanks that helped a lot i didn't see the extra lines because my line numbers is not on.
Dalton Coble
6,489 PointsCraig Dennis could you please help
Mark White
4,231 PointsWhen I was receiving this error I finally realized I had ''' String[] args=line.split("\/");
//should have been the pipe |
String[] args=line.split("\|"); '''