0
我做了下面給出的類來實現遊戲的「繪圖」並播放其中的聲音。執行時,我得到以下錯誤:NullPointerException嘗試播放音頻剪輯
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)
請一些人告訴我爲什麼我得到這個錯誤,以及如何解決它!
// Sound類
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();
}
}
}
// Game類 - 繪製遊戲
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
}
}
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –