0

I'm creating a AudioManager that list for me all audio from my "sounds" directory and insert them into my HashMap, but my app crash and shows a NullPointerException.

This is what I'm doing:

FileHandle[] files = Gdx.files.internal(FOLDER).list();
for (FileHandle file : files) {
    // NullPointerException
    this.sounds.put(file.nameWithoutExtension(),Gdx.audio.newSound(file));
}

I'm testing with my old android 2.3.6. That's the erro on logcat:

02-24 20:59:30.742: E/AndroidRuntime(3676): FATAL EXCEPTION: GLThread 10
02-24 20:59:30.742: E/AndroidRuntime(3676): java.lang.NullPointerException
02-24 20:59:30.742: E/AndroidRuntime(3676):     at com.climbup.system.AudioManager.<init>(AudioManager.java:40)
02-24 20:59:30.742: E/AndroidRuntime(3676):     at com.climbup.game.ClimbUp.create(ClimbUp.java:94)
02-24 20:59:30.742: E/AndroidRuntime(3676):     at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
02-24 20:59:30.742: E/AndroidRuntime(3676):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1361)
02-24 20:59:30.742: E/AndroidRuntime(3676):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1123)
02-24 20:59:30.757: E/SensorManager(3676): unregisterListener:: all sensors,  listener = com.badlogic.gdx.backends.android.AndroidInput$SensorListener@4052ff88
02-24 20:59:30.828: E/SensorManager(3676): unregisterListener:: all sensors,  listener = com.badlogic.gdx.backends.android.AndroidInput$SensorListener@40531668

What I'm doing wrong?

Jazzguy
  • 147
  • 1
  • 11
  • 1
    The backtrace for your exception includes more details, please include the message and stack trace. Please include the log from your app, if any. (Is this on Android or on your desktop?) To debug a NullPointerException it helps to know precisely which object is having a problem, so separating every deference onto a separate line will help. – P.T. Feb 24 '15 at 04:43
  • 2
    possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Tenfour04 Feb 24 '15 at 05:55
  • Which line is line 40? One of the "." dereferences on that line is dereferencing null. You can narrow the problem down by figuring out which reference is null. (E.g, `assert(Gdx != null)`, `assert(Gdx.audio != null)`, `assert(this.sounds != null)`, `assert(file != null)`, etc.) – P.T. Feb 25 '15 at 00:14
  • The line 40 refers to "this.sounds.put(file.nameWithoutExtension(), Gdx.audio.newSound(file));". I tried to use asserts but didn't work. – Jazzguy Feb 25 '15 at 00:34
  • The Sound is an interface. I'ts possible add an interface into a Hashmap? – Jazzguy Feb 25 '15 at 20:28

0 Answers0