Java Java Data Structures Efficiency! Custom Serialization

Rashadat Mirzayev
Rashadat Mirzayev
14,601 Points

Exception 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)

https://w.trhou.se/0f5kqf4pk9

maybe someone had related problem and solved?

6 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Right - 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.

Rashadat Mirzayev
Rashadat Mirzayev
14,601 Points

Great! Thks Steve) I found a whitespace at the end, inside of songs.txt file and deleted it. Now program is compiling.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

Glad it worked! :+1:

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.

Hi 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
Steve Hunter
Treehouse Moderator 57,551 Points

I'll have a look now - give me a minute ...

Steve.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

Hi 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
Steve Hunter
Treehouse Moderator 57,551 Points

Can you take another snapshot of your workspace, please? It isn't working for me at this end - no idea why!

Hey Steve

Here is the new snapshot https://w.trhou.se/1zppkgrwev

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

Thanks - I'm just cooking now. I'll be with you ASAP.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi there,

This might be because you used args[3] rather than args[2]; it's out of sequence?

yourcode.java
addSong(new Song(args[0], args[1], args[3]));

Steve.

Rashadat Mirzayev
Rashadat Mirzayev
14,601 Points

Hi Steve, I improved that mistake, but still can't run the program

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

OK - what error are you getting now? Copy the error messages and I'll see if I can fix them!!

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

When 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
Rashadat Mirzayev
14,601 Points

Thanks 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)

https://w.trhou.se/qbpui12lo6

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
Steve Hunter
Treehouse Moderator 57,551 Points

I'll have another look but I had your code working!

Steve Hunter
Steve Hunter
Treehouse Moderator 57,551 Points

OK - 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
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Clive - 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.

It worked : ) Thanks Man you a life saver.