NullPointerException - 音频 - 2D java平台游戏

时间:2015-04-05 23:44:28

标签: java audio nullpointerexception

我最近开始使用Java进行编码,并决定制作游戏 我已经开始使用2D平台游戏,并在线发现了一些音频.java文件并将其添加到

但是当我运行游戏时,我得到一个NullPointerException并且它真的让我烦恼为什么它不能正常工作

这是我的音频文件的代码:

package me.EuanGraphics.Audio;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class AudioPlayer {

private Clip clip;

public AudioPlayer(String s) {

    try {

        AudioInputStream ais =
                AudioSystem.getAudioInputStream(
                        getClass().getResourceAsStream(s)
                        );
        AudioFormat baseFormat = ais.getFormat();
        AudioFormat decodeFormat = new AudioFormat (
                AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(), false
            );
            AudioInputStream dais = 
                    AudioSystem.getAudioInputStream(
                            decodeFormat, ais);
            clip = AudioSystem.getClip();
            clip.open(dais);


    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void play() {
    if(clip == null) return;
    stop();
    clip.setFramePosition(0);
    clip.start();
}

public void stop() {
    if (clip.isRunning()) clip.stop();
}

public void close() {
    stop();
    clip.close();
}

}

这是我游戏中的菜单状态,用于调出音频文件并在进行选择时播放

package me.EuanGraphics.GameState;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import me.EuanGraphics.Entity.PlayerSave;
import me.EuanGraphics.Handler.Keys;
import me.EuanGraphics.Tilemap.Background;
import me.EuanGraphics.Audio.AudioPlayer;

public class MenuState extends GameState {

private BufferedImage head;
private Background menubg;
private AudioPlayer menuoption;
private AudioPlayer menuselect;

private int currentChoice = 0;
private String[] options = {
        "Start",
        "Quit"
};

private Color titleColor;
private Font titleFont;

private Font font;
private Font font2;

public MenuState(GameStateManager gsm) {
    super(gsm);

    try {

        //Load float head
        head = ImageIO.read(getClass().getResourceAsStream("/HUD/Hud.gif")).getSubimage(0, 12, 12, 11);
        menubg = new Background("/Backgrounds/Menu.png", 0);

        //Titles and fonts
        titleColor = Color.BLACK;
        titleFont = new Font("Time New Roman", Font.PLAIN, 20);
        font = new Font("Arial", Font.PLAIN, 14);
        font2 = new Font("Arial", Font.PLAIN, 10);


    }
    catch(Exception e) {
        e.printStackTrace();
    }
}


public void init() {

    menuoption = new AudioPlayer("/SFX/menuoption.wav");
    menuselect = new AudioPlayer("/SFX/menuselect.mp3");
}

public void update() {

    //Check Keys
    handleInput();

}


public void draw(Graphics2D g) {

    //Draw Background
    menubg.draw(g);

    // Draw Title
    g.setColor(titleColor);
    g.setFont(titleFont);
    g.drawString("Dragontale: Remastered", 55, 90);

    // Draw Menu Options
    g.setFont(font);
    g.setColor(Color.BLACK);
    g.drawString("Start", 145, 165);
    g.drawString("Quit", 145, 185);

    // Draw Floating Heads
    if(currentChoice == 0) g.drawImage(head, 125, 154, null);
    else if(currentChoice == 1) g.drawImage(head, 125, 174, null);

    //Other
    g.setFont(font2);
    g.drawString("2015 Euan P.", 10, 232);

}

private void select() {
    if(currentChoice == 0) {
        PlayerSave.init();
    }
    else if(currentChoice == 1) {
        System.exit(0);
    }
}

@Override
public void handleInput() {
    if(Keys.isPressed(Keys.ENTER)) select();
    if(Keys.isPressed(Keys.UP)) {
        if(currentChoice > 0) {
            menuoption.play();
            currentChoice--;
        }
    }
    if(Keys.isPressed(Keys.DOWN)) {
        if(currentChoice < options.length - 1) {
            menuoption.play();
            currentChoice++;
}
}
}
}

在我告诉代码播放音频文件时,它给了我一个错误 E.G menuoption.play()

由于 - 尤安

2 个答案:

答案 0 :(得分:0)

当应用程序在需要对象的情况下尝试使用null时,会发生NullPointerException。

也许在这里要做的事情:

 public void play() {
     if(clip == null) return;
     stop();
     clip.setFramePosition(0);
     clip.start(); }

http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

答案 1 :(得分:0)

写作:

private AudioPlayer menuoption;

等于

private AudioPlayer menuoption=null;

当您尝试从= null的对象调用方法时,您将获得NullPointerException。 因为你在调用menuoption.play()之前没有调用init()(事实上,你没有在任何地方调用init()),你正在尝试这样做。 尝试在MenuState的构造函数末尾添加init()。