Hydroid Hydroid - 11 days ago 5
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

*jar
.
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;
}


inputFile.listfiles()
returns null, as seems my path isn't OK.
But my path is Ok, because I let it print on the command line.
D:\Eclipse\MyProjekt\dist\MyProject.jar\audio
.
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

Are you sure this is the correct file path?

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

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.