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 trialRashadat Mirzayev
14,601 PointsException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
Cannot find what is the problem: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.teamtreehouse.model.SongBook.importFrom(SongBook.java:44) at Karaoke.main(Karaoke.java:9)
maybe someone had related problem and solved?
6 Answers
Steve Hunter
57,712 PointsRight - fixed.
For clarity, I added that code inside the importFrom()
method in the while
loop above the addSong()
method which was causing the issue.
I wanted to see what the array was being read as, as this was clearly the cause of the problem.
The output ends will a blank args[0]
which is reading all the whitespace at the end of songs.txt
. That ends the file read with a blank args[0]
as you can see in the output from my debug code. But the code requires a complete line. Because the file read had run out after args[0]
, this means that args[1]
and args[2]
don't exist! That's what causes your problem.
So, ensuring there's no whitespace at the end of the list of songs will prevent this problem recurring.
I hope that makes sense.
Steve.
Steve Hunter
57,712 PointsHi there,
This might be because you used args[3]
rather than args[2]
; it's out of sequence?
addSong(new Song(args[0], args[1], args[3]));
Steve.
Rashadat Mirzayev
14,601 PointsHi Steve, I improved that mistake, but still can't run the program
Steve Hunter
57,712 PointsOK - what error are you getting now? Copy the error messages and I'll see if I can fix them!!
Steve Hunter
57,712 PointsWhen I change your code to args[2]
instead of args[3]
it works fine.
Before I changed to code, the error said ArrayOutOfBoundsException: 3
which suggested to me that it was this issue.
With that small alteration, the code is working fine.
Steve.
Rashadat Mirzayev
14,601 Pointsthe same error, nothing changes.
Rashadat Mirzayev
14,601 PointsThanks Steve for helping. This is the exception it shows when I try to compile: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at com.teamtreehouse.model.SongBook.importFrom(SongBook.java:44) at Karaoke.main(Karaoke.java:9)
I know the problem is somewhere in getting the items from array, but can not find it. Still doesn't want to work.
Steve Hunter
57,712 PointsI'll have another look but I had your code working!
Steve Hunter
57,712 PointsOK - it isn't stopping reading when the songs run out. I added:
System.out.printf("args[0]: %s \t", args[0]);
System.out.printf("args[1]: %s \t", args[1]);
System.out.printf("args[2]: %s \n", args[2]);
Which gives me (I shortened the output - it gives me all the songs):
args[0]: The Cure args[1]: Love Song args[2]: https://www.youtube.com/watch?v=ogi6aMOYOEQ
args[0]: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
So, this isn't stopping the readLine
and is filling null elements, I think.
Steve Hunter
57,712 PointsClive - I think I got this working.
It's difficult to check because my internet connection has a very high latency which Workspaces really can't compensate for.
The issue is on your splitting of the songs. You've got:
String[] args = line.split("\\}");
^
I think it should be
String[] args = line.split("\\|");
You want the pipe, not the curly brace.
Try it at your end and let me know how you get on.
Steve.
Clive Shenje
2,585 PointsIt worked : ) Thanks Man you a life saver.
Rashadat Mirzayev
14,601 PointsRashadat Mirzayev
14,601 PointsGreat! Thks Steve) I found a whitespace at the end, inside of songs.txt file and deleted it. Now program is compiling.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsGlad it worked!
And think about how the solution was arrived at ... using little tricks like that will definitely help in the future when figuring out what's wrong with code.
Steve.
Clive Shenje
2,585 PointsClive Shenje
2,585 PointsHi Steve,
I have the same problem as Rashadat, if I may ask, where exactly is the whitespace at the end of songs.txt
https://w.trhou.se/rid3q7f80r
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI'll have a look now - give me a minute ...
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Clive,
You don't seem to have any whitespace in your file so your issue is different to the one in this thread.
I'm trying to fork your Workspace but it won't let me for some reason. Once I've sorted that I'll see if I can sort the issue.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsCan you take another snapshot of your workspace, please? It isn't working for me at this end - no idea why!
Clive Shenje
2,585 PointsClive Shenje
2,585 PointsHey Steve
Here is the new snapshot https://w.trhou.se/1zppkgrwev
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThanks - I'm just cooking now. I'll be with you ASAP.