Sound cannot be play

I am trying to play the sound in my Main.java,but when I start the game it show out a warning:

[java]
Jan 22, 2020 6:03:04 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.2-stable

  • Branch: HEAD
  • Git Hash: 8291d61
  • Build Date: 2019-07-27
    Jan 22, 2020 6:03:04 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
    INFO: LWJGL 2.9.3 context running on thread jME3 Main
  • Graphics Adapter: null
  • Driver Version: null
  • Scaling Factor: 1
    Jan 22, 2020 6:03:04 PM com.jme3.renderer.opengl.GLRenderer loadCapabilitiesCommon
    INFO: OpenGL Renderer Information
  • Vendor: Intel Open Source Technology Center
  • Renderer: Mesa DRI Intel® Sandybridge Desktop
  • OpenGL Version: 3.0 Mesa 19.0.8
  • GLSL Version: 1.30
  • Profile: Compatibility
    Jan 22, 2020 6:03:05 PM com.jme3.asset.AssetConfig loadText
    WARNING: Cannot find loader com.jme3.scene.plugins.blender.BlenderModelLoader
    Jan 22, 2020 6:03:05 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
    INFO: Audio Renderer Information
  • Device: OpenAL Soft
  • Vendor: OpenAL Community
  • Renderer: OpenAL Soft
  • Version: 1.1 ALSOFT 1.15.1
  • Supported channels: 64
  • ALC extensions: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_loopback
  • AL extensions: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points AL_SOFT_source_latency
    Jan 22, 2020 6:03:05 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
    WARNING: Pausing audio device not supported.
    Jan 22, 2020 6:03:05 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
    INFO: Audio effect extension version: 1.0
    Jan 22, 2020 6:03:05 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
    INFO: Audio max auxiliary sends: 4
    Jan 22, 2020 6:03:05 PM com.jme3.app.LegacyApplication handleError
    SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
    java.lang.NullPointerException
    at mygame.Main.simpleInitApp(Main.java:78)
    at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:220)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:211)
    at java.lang.Thread.run(Thread.java:748)
    [/java]

Because the music is write in another class,so I create a private Sound sound; to call the method that I had write in the Sound.java. Here is my code to call it:

sound.startMusic();

Does anything wrong here ?

We would need to see the code that created the audionode and plays it. The line that throws the nullpointer is null. Nullpointers are amongst the easiest problems to solve.

As Jay says, this is where your bug is:

Here is the code that I create it:

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import java.util.Random;

/**
 *
 * @author simon
 */
public class Sound {
    private AudioNode music;
    private AudioNode[] shots;
    private AudioNode[] explosions;
    private AudioNode[] spawns;

    private AssetManager assetManager;

public Sound(AssetManager assetManager) {
    this.assetManager = assetManager;
    shots = new AudioNode[4];
    explosions = new AudioNode[8];
    spawns = new AudioNode[8];

    loadSounds();
}

private void loadSounds() {
    music = new AudioNode(assetManager,"Sounds/Music.ogg");
    music.setPositional(false);
    music.setReverbEnabled(false);
    music.setLooping(true);

    for (int i=0; i<shots.length; i++) {
        shots[i] = new AudioNode(assetManager,"Sounds/shoot-0"+(i+1)+".wav");
        shots[i].setPositional(false);
        shots[i].setReverbEnabled(false);
        shots[i].setLooping(false);
    }

    for (int i=0; i<explosions.length; i++) {
        explosions[i]  = new AudioNode(assetManager,"Sounds/explosion-0"+(i+1)+".wav");
        explosions[i].setPositional(false);
        explosions[i].setReverbEnabled(false);
        explosions[i].setLooping(false);
    }

    for (int i=0; i<spawns.length; i++) {
        spawns[i]  = new AudioNode(assetManager,"Sounds/spawn-0"+(i+1)+".wav");
        spawns[i].setPositional(false);
        spawns[i].setReverbEnabled(false);
        spawns[i].setLooping(false);
    }
}

public void startMusic() {
    music.play();
}

public void shoot() {
    shots[new Random().nextInt(shots.length)].playInstance();
}

public void explosion() {
    explosions[new Random().nextInt(explosions.length)].playInstance();
}

public void spawn() {
    spawns[new Random().nextInt(spawns.length)].playInstance();
}
}

The code is:

sound.startMusic();

Did I using wrong method or what ?

sound is null

…you haven’t even shown us that code. The bug is in the code you didn’t post. The one that should have set “sound” to something and didn’t.

Did I using wrong method or what ?

yes, you use method on null value.

null.startMusic(); will not work for sure.

its most probably in mygame.Main.simpleInitApp(Main.java:78) line, so this is your code issue.

we cant know what you have in this line, its like “guess what i think”
so you want us to guess what is in mygame.Main.simpleInitApp

private Sound sound;
...
sound.startMusic();

@pspeed @oxplay2
Did you say the ‘code’ is this ?
I just type it in my Main.java, and the line 78 is the sound.startMusic;

And you guys say the Sound is null maybe because I don’t add new to it ?

Yes, sound is null because you’ve never set it to anything.

Learning to program in Java is like learning to ride a bike.
Learning to program 3D games is like learning to shave with a straight-razor.

Trying to do both at once is extremely painful.

1 Like
private Sound sound = new Sound;
private Sound sound = Sound.class;

I try to set it like above, but both of it goes wrong.
Had any another way to set Sound up ?

Without understanding how to code in Java, trying random things isn’t going to be very productive.

I recommend that you run through some basic Java tutorials until you have a better grasp of the language and how to program. The rest of your journey will go much more easily.

All right , I will do it. And thanks for all of your guy’s reply. :smiley:

1 Like

Do not be discourage. The error is very obvious and giving you the answer would not be of help, you would just lurch onto the next problem.

Try this,

https://www.w3schools.com/java/default.asp

and this one, although old it’s written by a very good author, Doug Lowe,

https://www.amazon.com/Java-All-One-Dummies-Computers/dp/1119247799/ref=dp_ob_title_bk

New or used.

Anyway, they are easy reads and will get you going.

2 Likes