1

The "How to solve a nullpointerException" answer in no way solved this problem. I know what a null pointer exception is. Why is a reference to an mp3 file in libGdx, in Android studio, creating a null pointer exception.

So I am doing the Brent Aureli's Code School tutorial of making flappy bird with libGdx. I am on video 14. https://www.youtube.com/watch?v=EaOE8rFa_8Q At 2:50 in the video he shows his code for this. I keep getting this error : "LwjglApplication: Couldn't initialize audio, disabling audio" when running the program after I add the libGdx "Music" object. Here is the full error list. Everything above "Exception in thread "LWJGL Application" java.lang.NullPointerException" Has already been showing up but the app still ran. Now it is getting this null pointer exception at the line:

music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));

in the create() method. I have looked everywhere for an answer to this. I tried to copy and paste the mp3 manually into the raw directory or assets directory. That doesn't work. And here: Why does android studio not recognize .wav / .mp3 files, is a good example of how this problem is being handled. My code:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.states.GameStateManager;
import com.mygdx.game.states.MenuState;

public class FlappyDemo extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;

public static final String TITLE = "Flappy Bird";

private GameStateManager gsm;
private SpriteBatch batch;
Texture img;
private Music music;

@Override
public void create () {
    batch = new SpriteBatch();
    gsm = new GameStateManager();
    music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
    music.setLooping(true);
    music.setVolume(0.1f);
    music.play();
    gsm.push(new MenuState(gsm));
    Gdx.gl.glClearColor(1, 0, 0, 1);

}

@Override
public void render () {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.render(batch);

}

@Override
public void dispose () {
    batch.dispose();
    img.dispose();
    music.dispose();
}

}

All the png files in assets work just fine. Also any mp3's I put in Android Studio have a "?" symbol as the icon If anyone has any idea it would be much appreciated.

  • Sorry here is the error list: – Jeremiah Jacobson Jun 21 '17 at 22:42
  • where you kept your `music.mp3` file ? – Abhishek Aryan Jun 22 '17 at 05:02
  • Does the computer you're using have a sound card? I had a similar issue on my work pc where it has no audio card or audio drivers so it was unable to load any sounds and caused the app to crash. – dfour Jun 22 '17 at 10:01
  • The file is in assets. I also tried it in a raw folder within res. Same thing. Also i do have a sound card with updated drivers – Jeremiah Jacobson Jun 22 '17 at 17:51
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ken White Jun 23 '17 at 01:38
  • Sorry Ken. Not a duplicate of https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. Try again – Jeremiah Jacobson Jun 23 '17 at 01:44
  • 1. What paths did you try (i.e. "raw/music.mp3","assets/music.mp3" etc) and did you find the file in the apk generated (unzip the apk). 2. Have you tried a standard Java File Handle (not GDX wrapped) to get a reference to the file? – Morrison Chang Jun 23 '17 at 02:55
  • Tried res, raw, and asset paths. This happens with any mp3 file i put in there. And yeah tried the standard Java File Handle – Jeremiah Jacobson Jun 23 '17 at 13:16

0 Answers0