-2

So I'm creating this program that reads an MP3 file using a file input stream. After doing a lookup on the internet of my issue, it has to do with the array as shown here, however, I do not know how to fix this issue because I am a beginner in Java: File song = new File(arguments[0]);

I then come across this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.java24hours.ID3Reader.main(ID3Reader.java:11)

Here is my code:

package com.java24hours;
import java.io.*;



public class ID3Reader {

    private static String[] arguments;
    public static void main(String[] arguments) {

        File song = new File(arguments[0]);
        try (FileInputStream file = new FileInputStream(song)) {
            int size = (int) song.length();
            file.skip(size - 128);
            byte [] last128 = new byte[128];
            file.read(last128);
            String id3 = new String(last128);
            String tag = id3.substring(0, 3);
            if(tag.equals("TAG")) {
                System.out.println("Title: " + id3.substring(3, 32));
                System.out.println("Artist: " + id3.substring(33, 62));
                System.out.println("Album: " + id3.substring(63, 91));
                System.out.println("Year: " + id3.substring(93, 97));
            } else {
                System.out.println(arguments[0] + " does not contain " + 
                        " ID3 info.");
            }
            file.close();

        } catch (IOException ioe) {
            System.out.println("Error -- " + ioe.toString());
        }
    }


}

Can anyone help me on this? Thank you!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Dany
  • 1
  • 1
  • your formatting is a bit off... which line in your code is is line 11? – Ben M Jul 27 '17 at 03:22
  • how are you running this code? did you pass a file name as input? – transient_loop Jul 27 '17 at 03:22
  • Derp, yep, arguments[0] does not exist, so when you attempt to dereference it it fails. You should add a check for that, like: `if (arguments.length == 0) { throw new Exception("No arguments provided"); }` – Ben M Jul 27 '17 at 03:24
  • Another possible problem is that this code only deals with ID3v1 metadata. ID3v2 metadata is at the start of the file, and the structure is very different; see https://en.wikipedia.org/wiki/ID3. – Stephen C Jul 27 '17 at 03:30
  • 1
    However .... *"I do not know how to fix this issue because I am a beginner in Java ..."* - It is not appropriate for you to ask StackOverflow to fix bugs in programs for you. Even if you are a beginner. This is a Q&A site not a free programming service. – Stephen C Jul 27 '17 at 03:33
  • Here is a generic answer on how to fix ArrayIndexOutOfBounds errors: https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Stephen C Jul 27 '17 at 03:36

1 Answers1

0

You didn't provide a file name to your program.

This program expects a file name - the Mp3 file name - as an argument, which will be put in arguments[0].

If you run the program without arguments: java com.java24hours.ID3Reader

you get exactly this output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.java24hours.ID3Reader.main(ID3Reader.java:11)

because the arguments array is empty.

You need to provide a file name when calling this program, e.g.

java com.java24hours.ID3Reader TheDarkSideOfTheMoon.mp3

Where TheDarkSideOfTheMoon.mp3 must be a valid file.

If the file name is not present on your harddisk, you'll get a Error -- java.io.FileNotFoundException: TheDarkSideOfTheMoon.mp3 (No such file or directory).

You should put some more checks in your code!

transient_loop
  • 5,984
  • 15
  • 58
  • 117