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

标签 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++;
}
}
}
}

在我告诉代码播放音频文件的地方出现错误 例如menuoption.play()

谢谢 - 尤安

最佳答案

当应用程序在需要对象的情况下尝试使用 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

关于java - NullPointerException - 音频 - 2D java 平台游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463525/

相关文章:

python - 带有 pygame.mixer 音频的 Raspberry pi 仅产生静态

c# - 使用 .NET 检测 .ogg 文件属性? channel 数,每 channel 位数,采样率?

android - 包含 fragment 的框架布局中的 View 的 findViewById 失败

java - SceneBuilder 坐标与 JavaFX 坐标

java - 通过 ObjectOutputStream 发送相同但已修改的对象

Java ImageIO 删除图像后不会释放内存

iphone - 有什么方法可以在屏幕锁定时在 iPhone 上播放音频,但在设置静音开关时不能播放?

java - 使用 Intent putExtra 的空指针异常

android - 空对象引用上的 getApplicationContext()

java - 如何将 @RequestParam Map<String, String> 转换为自定义 Map<>