简体   繁体   中英

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

But when ever I run the game i get a NullPointerException and its really annoying me on why its not working

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

Thanks - Euan

The NullPointerException is made when an application attempts to use null in a case where an object is required.

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

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. Because you're not call init() before you call menuoption.play() (In fact, you're not calling init() anywhere) you are trying exactly that. Try adding init() at the end of your constructor for MenuState.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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