尝试播放AudioClip时出现NullPointerException

时间:2012-10-29 08:19:03

标签: java audio

我制作了下面给出的课程来实现游戏的“绘图”并在其中播放声音。执行时,我收到以下错误:

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
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

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

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