Hydroid Hydroid - 1 year ago 53
Java Question

NullPointer Exception while read audio files

I made a program to read audio files.
At first I did it with absolute path because it's easyer to develop.

Then I cahnge it to relative path because I want to compress it to a

So I code this method(at first only short later the code:

1.: at first I make a FileArray to save the Files
2.: make the array to save the AudioClip
3.: for loop to read the Clips

Now the code:

private AudioClip[] liesAudioDateien (File inputFile) {
File[] dateFileArray;
AudioClip[] tracks;
dateFileArray = inputFile.listFiles();
tracks = new AudioClip[dateFileArray.length];
for (int i = 0; i < tracks.length; i++) {
if (dateFileArray[i].isFile()) {
try {
tracks[i] = Applet.newAudioClip(dateFileArray[i].toURL());
} catch (IOException ex) {
System.err.println("Error!: -- " + ex.toString());

return tracks;

returns null, as seems my path isn't OK.
But my path is Ok, because I let it print on the command line.
In NetBeans, it does work.
If I make a jar file, it doesn't work.

I have already try:

  1. D:\Eclipse\MyProjekt\dist\MyProject.jar\audio\

  2. /
    in place of

Answer Source

Are you sure this is the correct file path?


The audio directory is in a folder called MyProject.jar?

If your audio files are within the .jar file you cannot use the File class to list them. What you have to do instead is read the entries from the .jar file, like in this question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download