gpt4 book ai didi

java - 尝试播放 AudioClip 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 00:09:36 25 4
gpt4 key购买 nike

我创建了下面给出的类来实现游戏的“绘图”并在其中播放声音。执行时出现以下错误:

java.lang.NullPointerException
at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:65)
at java.applet.Applet.newAudioClip(Applet.java:311)
at AllPanels.Sound.<init>(Sound.java:20)
at AllPanels.Sound.<clinit>(Sound.java:15)
at AllPanels.BailoutGame.mousePressed(BailoutGame.java:124)
at java.awt.Component.processMouseEvent(Component.java:6502)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Window.processEvent(Window.java:2022)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

请有人告诉我为什么会出现此错误以及如何解决它!!

//声音类

package AllPanels;

import java.applet.Applet;
import java.applet.AudioClip;

public class Sound {
public static final Sound fire = new Sound("/home/vivek/Projects/Java/SniperGame/src/Sounds/50 Cal.wav");
private AudioClip clip;

public Sound(String fileName){
try{
clip = Applet.newAudioClip(Sound.class.getResource(fileName));
}catch(Exception e) {
System.out.println(e);
e.printStackTrace();
}
}

public void play() {
try{
new Thread(){
public void Run(){
clip.play();
}
}.start();
}catch(Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}

//游戏类-绘制游戏

package AllPanels;

import java.awt.*;
import java.awt.event.*;
public class BailoutGame extends Core implements MouseListener, MouseMotionListener, KeyListener {

public static void main(String args[]) {
(new Thread(new BailoutGame())).start();
}

//Variables

@Override
public void init(){
super.init();
Window w = s.getFullScreenWindow();
w.addKeyListener(this);
w.addMouseListener(this);
w.addMouseMotionListener(this);
}

@Override
public synchronized void draw(Graphics2D g) {
//some code
}

@Override
public void mouseReleased(MouseEvent e) { }

@Override
public void mouseClicked(MouseEvent e) { }

@Override
public void mouseEntered(MouseEvent e) { }

@Override
public void mouseExited(MouseEvent e) { }

@Override
public void mouseDragged(MouseEvent e) { }

@Override
public void keyTyped(KeyEvent e) { }

@Override
public void keyReleased(KeyEvent e) { }

@Override
public void mousePressed(MouseEvent e) {
if((e.getButton() == MouseEvent.BUTTON1) && bulletsInClip != 0){
Sound.fire.play();
//some Code
}
if(e.getButton() == MouseEvent.BUTTON3){
reload();
}
}

@Override
public void mouseMoved(MouseEvent e) {
//some Code
}

@Override
public void keyPressed(KeyEvent e) {
//some Code
}
}

最佳答案

我强烈怀疑 getResource(filename) 返回 null

请注意,Class.getResource 不采用完整的文件名 - 它采用相对于类或类加载器根的资源名称。试试这个:

public static final Sound fire = new Sound("/Sounds/50 Cal.wav");

假设 Sounds 目录嵌入到以 src 作为资源根的资源中,这应该可以工作。

关于java - 尝试播放 AudioClip 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13118018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com