I'm working out of the book Learning Libgdx Game Development on Chapter 10 dealing with adding music and sound to the game. I tried Googling around to see if anyone else came across the same null pointer issue with adding the music but no luck.
Essentially trying to load in a Music track from a different class called Assets to run in my main game.
I see the assets are loaded, it prints out on the console. So I don't think it is an issue with not finding the file.
com.woodgdx.game.Assets: # of assets loaded: 9
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump_with_feather.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.png
com.woodgdx.game.Assets: asset: ../core/assets/music/keith303_-_brand_new_highscore.mp3
com.woodgdx.game.Assets: asset: ../core/assets/sounds/live_lost.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack2.png
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_feather.wav
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.atlas
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_coin.wav
So I'm not really sure what is going on...
The error itself is..
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.woodgdx.game.woodGdxGame.create(woodGdxGame.java:29)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Main class
package com.woodgdx.game;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.woodgdx.game.Assets;
import com.woodgdx.game.screens.MenuScreen;
import com.woodgdx.game.util.AudioManager;
import com.woodgdx.game.util.GamePreferences;
/**
* The main game file with the main
* engine methods
* @author xxxx
*/
public class woodGdxGame extends Game
{
@Override
public void create()
{
// Set Libgdx log level
Gdx.app.setLogLevel(Application.LOG_DEBUG);
// Load assets
Assets.instance.init(new AssetManager());
// Load preferences for audio settings and start playing music
GamePreferences.instance.load();
AudioManager.instance.play(Assets.instance.music.song01);
// Start game at menu screen
setScreen(new MenuScreen(this));
}
}
The error occurs on this line..
AudioManager.instance.play(Assets.instance.music.song01);
My Assets class contains this inner class. The way the book had it, I got the same error when I made it it's own class.
/**
* Music for game loaded
* @author xxxx
*
*/
public class AssetMusic
{
//Our music object
public final Music song01;
/**
* Retrieves the music file for the music
* @param am
*/
public AssetMusic(AssetManager am)
{
song01 = am.get("../core/assets/music/keith303_-_brand_new_highscore.mp3", Music.class);
}
}
Then here is the AudioManager class they does play a little role in here.
/**
* Manages the playing and organization
* of audio files
* @author xxxx
*
*/
public class AudioManager
{
public static final AudioManager instance = new AudioManager();
private Music playingMusic;
// singleton: prevent instantiation from other classes
private AudioManager()
{
}
/**
* Play sound with default vol, pitch, and pan (0)
* @param sound
*/
public void play(Sound sound)
{
play(sound, 1);
}
/**
* Play sound with default pitch and pan (0) but different vol
* @param sound
* @param volume
*/
public void play(Sound sound, float volume)
{
play(sound, volume, 1);
}
/**
* Play sound with default pan (0) with different pitch and vol
* @param sound
* @param volume
* @param pitch
*/
public void play(Sound sound, float volume, float pitch)
{
play(sound, volume, pitch, 0);
}
/**
* Plays sound with a vol, pitch, and pan activated
* @param sound
* @param volume
* @param pitch
* @param pan
*/
public void play(Sound sound, float volume, float pitch, float pan)
{
//If sound is muted, dont play
if (!GamePreferences.instance.sound)
return;
sound.play(GamePreferences.instance.volSound * volume, pitch, pan);
}
/**
* Plays music on loop
* @param music
*/
public void play(Music music)
{
stopMusic();
playingMusic = music;
if (GamePreferences.instance.music)
{
music.setLooping(true);
music.setVolume(GamePreferences.instance.volMusic);
music.play();
}
}
/**
* Ends the music loop of doom
*/
public void stopMusic()
{
if (playingMusic != null)
playingMusic.stop();
}
/**
* Checks settings for muted music, vol level
*/
public void onSettingsUpdated()
{
//No music available
if (playingMusic == null)
return;
//Sets volume
playingMusic.setVolume(GamePreferences.instance.volMusic);
//Checks if music is not muted
if (GamePreferences.instance.music)
{
//If music isn't playing, play it
if (!playingMusic.isPlaying())
playingMusic.play();
}
//Mute music if selected so
else
{
playingMusic.pause();
}
}
}