简体   繁体   English

NullPointerException-音频-2D Java Platformer

[英]NullPointerException - Audio - 2D java platformer

I have Recently start coding in Java and decided to have a crack at making games I have started with a 2D platformer, and found some audio .java files online and added them in 我最近开始用Java进行编码,并决定尝试制作我从2D平台开始的游戏,并在网上找到一些音频.java文件并将其添加到

But when ever I run the game i get a NullPointerException and its really annoying me on why its not working 但是,每当我运行游戏时,我都会收到NullPointerException,这真的使我烦恼,为什么它不起作用

Here is my code for my audio file: 这是我的音频文件的代码:

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();
}

} }

and here is my Menu state in my game that is meant to call up the Audio file and play it when you make a selection 这是我游戏中的菜单状态,用于在选择时调用音频文件并播放

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

It give me an error at where I am telling the code to play the audiofile EG menuoption.play() 它在告诉代码播放音频文件EG menuoption.play()的地方给了我一个错误。

Thanks - Euan 谢谢-阮

The NullPointerException is made when an application attempts to use null in a case where an object is required. 当应用程序在需要对象的情况下尝试使用null时,将产生NullPointerException。

Maybe something to do in here: 也许在这里要做的事情:

 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 http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

Writing: 写作:

private AudioPlayer menuoption;

is equal to 等于

private AudioPlayer menuoption=null;

When you try to call a method from an object that is =null you will get a NullPointerException. 当您尝试从= null的对象调用方法时,将收到NullPointerException。 Because you're not call init() before you call menuoption.play() (In fact, you're not calling init() anywhere) you are trying exactly that. 因为您没有在调用menuoption.play()之前调用init()(实际上,您并未在任何地方调用init()),所以您正尝试这样做。 Try adding init() at the end of your constructor for MenuState. 尝试在MenuState的构造函数的末尾添加init()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM