0

i convert a mp3 file into byte array and i read from byte array but it shows null pointer exception on line number 15 my code:

public class MainClass {
    static byte[] bytesarray = null;

    public static void main(String args[]) {
        try {
            FileInputStream fis = new FileInputStream("D:\\taxi.mp3");
            try {
                fis.read(bytesarray, 0, 32);
                System.out.println(bytesarray.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ByteArrayInputStream in = new ByteArrayInputStream(bytesarray);
        for (int i = 0; i < 32; i++) {
            int c;
            while ((c = in.read()) != -1) {
                if (i == 0) {
                    System.out.print((char) c);
                } else {
                    System.out.print(Character.toUpperCase((char) c));
                }
            }
            System.out.println();

        }
    }
}

2 Answers 2

3

static byte[] bytesarray = new byte[32]; should do the work, you didn't initialize your array...

See the documentation of read.

Sign up to request clarification or add additional context in comments.

1 Comment

If you don't hardcoded the array to null like this: "static byte[] bytesarray = null;", then you might actually get warned that you are trying to access an uninitialized variable. For this reason, it's best practice in Java NOT to initialize your values to null, false etc. Unlike in C where this is actually recommended...
1
static byte[] bytesarray = new byte[32];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.