英文:
NullPointerException when trying to play sound
问题
代码:
public static void main(String[] args) {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Main.class.getResource("ProgramAudio\\chechen.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
错误:
java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1032)
at aChatProgramAudio.audio2.main(audio2.java:13)
也许给定的目录与此有关,但它确实存在。
英文:
I'm having a simple program that plays a sound when running it.
code:
public static void main(String[] args) {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Main.class.getResource("ProgramAudio\\chechen.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Error:
java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1032)
at aChatProgramAudio.audio2.main(audio2.java:13)
Maybe the given directory has something to do with it but it does exists.
答案1
得分: 1
以下是翻译好的内容:
在这个示例中,将 chechen.wav
放在与 Main.java
相同的包文件夹中。
URL url = Main.class.getResource("chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
如果你的包文件夹结构类似于以下内容(其中 ProgramAudio
是位于 src
下的一个包):
src/yourProgramCodePackage/Main.java
src/ProgramAudio/chechen.wav
那么以下代码应该可以工作,使用 "绝对" 寻址方式。
URL url = Main.class.getResource("/ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
如果你的文件夹结构如下,其中 ProgramAudio
是代码包中包含有 Main.java
的子文件夹,则使用 "相对" 寻址方式应该可以工作。
src/yourProgramCodePackage/Main.java
src/yourProgramCodePackage/ProgramAudio/chechen.wav
URL url = Main.class.getResource("ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
可能是一个不错的主意遵循包名称的约定,保持使用小写或驼峰命名,例如,如果将其用作包名,则使用 programAudio
。
英文:
Some possibilities to try.
In this one, have chechen.wav
in the same package folder as Main.java
.
URL url = Main.class.getResource("chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
If your package folder structure is something like the following (where ProgramAudio
is a package under src
):
src/yourProgramCodePackage/Main.java
src/ProgramAudio/chechen.wav
Then the following should work, using "absolute" addressing.
URL url = Main.class.getResource("/ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
If you have the following folder structure, where ProgramAudio
is a subfolder of the code package with Main.java
in it, then the "relative" form of addressing should work.
src/yourProgramCodePackage/Main.java
src/yourProgramCodePackage/ProgramAudio/chechen.wav
URL url = Main.class.getResource("ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
Probably a good idea to follow convention for package names and stick with lower or camel case, e.g., programAudio
if that is being used as a package name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论