[英]NullPointerException when loading file from res folder - Java
所以我用這段代碼:
ClassLoader classLoader = getClass().getClassLoader();
File audioFile = new File(classLoader.getResource("pop.wav").getFile());
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.open(audioStream);
audioClip.start();
但是每次它給我NullPointerException。 文件(pop.wav)位於src / res中。 但是,如果將它放在我的桌面上,而不是放在classLoader.getResource("pop.wav").getFile()
我會把它放在C:/Users/gebruiker/Desktop/pop.wav
。
資源不是文件。
嘗試這個:
AudioInputStream audioStream = AudioSystem.getAudioInputStream(getClass ().getResourceAsStream ("/res/pop.wav"));
並檢查該pop.wav的原因路徑。 有時.jar(/pop.wav)中的絕對路徑更易於管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.