空指针异常:尝试播放声音时发生

huangapple go评论114阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月2日 00:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63691829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定