Andrew Wilken
Andrew Wilken

Reputation: 55

NullPointerException whe reading audio from JAR

I tried to make a runnable JAR, but for some reason I couldn't get my game to play. I did some research and ran it through my command prompt to try to find the error and I got this below. So obviously I know the issue I just need to fix it. I am new to programming so not quite sure what this is telling me.

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.Objects.requireNonNull(Unknown Source)
        at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
        at builder.AudioPlayer.playMenuSound(AudioPlayer.java:20)
        at builder.Game.<init>(Game.java:56)
        at builder.Game.main(Game.java:61)
package builder;

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlayer {

    private static Clip play;

    public static void playMenuSound()
    {
        try {
            //AudioInputStream menuSound = AudioSystem.getAudioInputStream(new File("src/res/introSong.wav")); //Take in audio from res folder
            AudioInputStream menuSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("res/introSong.wav"));
            play = AudioSystem.getClip(); //
            play.open(menuSound); //Play the sound
            FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
            volume.setValue(1.0f); //0.0 - 1.0 volume
            play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
        }catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
            e.printStackTrace();
        }
    }

    public static void playGameSound()
    {
        try {
            //AudioInputStream gameSound = AudioSystem.getAudioInputStream(new File("src/res/inGame.wav")); //Take in audio from res folder
            AudioInputStream gameSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResourceAsStream("res/inGame.wav"));
            play = AudioSystem.getClip(); //
            play.open(gameSound); //Play the sound
            FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN); //Get control of volume
            volume.setValue(0.5f); //0.0 - 1.0 volume
            play.loop(Clip.LOOP_CONTINUOUSLY); //Loop once clip is over
        }catch (LineUnavailableException | IOException | UnsupportedAudioFileException e){
            e.printStackTrace();
        }
    }

    public static void stopMusic()
    {
        play.close(); //Stop music
    }
}

Upvotes: 0

Views: 102

Answers (1)

dan1st
dan1st

Reputation: 16467

Your sound file inGame.wav is located in a directory res inside your source directory.

When you export it, the sources will be compiled and copied to the JAR.

This results in the sound file being in a subdirectory res inside the JAR.

You try to read the file inGame.wav but you have to read res/inGame.wav instead.

The second problem is that the system cannot set markers on the InputStream. This can be solved by changing getResourceAsStream to getResource().

AudioInputStream menuSound = AudioSystem.getAudioInputStream(AudioPlayer.class.getClassLoader().getResource("res/inGame.wav"));

Upvotes: 1

Related Questions