[NiftyGui] NullPointerException when trying to switch screen

Hi,

I’m trying to make an interface with NiftyGUI. But i meet a problem :

I initialize Nifty and NiftyDisplay, so they are not null, of course.
When I click on the Play button, it must execute the play() method.
But when I click it, it seems to create a new instance of my class and don’t get the values of the first instance.
When I verify the values of Nifty and NiftyDisplay (and a test int), they

I know it’s not very clear so I give you my error console :

[java]run:
mai 02, 2014 1:39:49 PM java.util.prefs.WindowsPreferences <init>
Avertissement: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(…) returned error code 5.
mai 02, 2014 1:39:50 PM com.jme3.system.JmeDesktopSystem initialize
Infos: Running on jMonkeyEngine 3.0.5
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: Lwjgl 2.9.0 context running on thread LWJGL Renderer Thread
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: Adapter: atiu9p64
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: Driver Version: 8.14.1.6150
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: Vendor: ATI Technologies Inc.
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: OpenGL Version: 4.0.10243 Compatibility Profile Context
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: Renderer: AMD Radeon HD 6650M
mai 02, 2014 1:39:51 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
Infos: GLSL Ver: 4.00
mai 02, 2014 1:39:51 PM com.jme3.asset.AssetConfig loadText
Avertissement: Cannot find loader com.jme3.scene.plugins.blender.BlenderModelLoader
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio Device: OpenAL Soft
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio Vendor: OpenAL Community
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio Renderer: OpenAL Soft
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio Version: 1.1 ALSOFT 1.15.1
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: AudioRenderer supports 64 channels
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio effect extension version: 1.0
mai 02, 2014 1:39:51 PM com.jme3.audio.lwjgl.LwjglAudioRenderer initInThread
Infos: Audio max auxilary sends: 4
0
2null com.jme3.niftygui.NiftyJmeDisplay@1b5b7a9f
3de.lessvoid.nifty.Nifty@12cf6ac1 com.jme3.niftygui.NiftyJmeDisplay@1b5b7a9f
4de.lessvoid.nifty.Nifty@12cf6ac1 com.jme3.niftygui.NiftyJmeDisplay@1b5b7a9f
5de.lessvoid.nifty.Nifty@12cf6ac1 com.jme3.niftygui.NiftyJmeDisplay@1b5b7a9f
6de.lessvoid.nifty.Nifty@12cf6ac1 com.jme3.niftygui.NiftyJmeDisplay@1b5b7a9f
1
1null null
0
java.lang.NullPointerException
at mygame.MyStartScreen.play(MyStartScreen.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at de.lessvoid.nifty.NiftyMethodInvoker.callMethod(NiftyMethodInvoker.java:145)
at de.lessvoid.nifty.NiftyMethodInvoker.performInvoke(NiftyMethodInvoker.java:104)
at de.lessvoid.nifty.Nifty$DelayedMethodInvoke.perform(Nifty.java:1174)
at de.lessvoid.nifty.Nifty.invokeMethods(Nifty.java:1152)
at de.lessvoid.nifty.Nifty.handleDynamicElements(Nifty.java:354)
at de.lessvoid.nifty.Nifty.access$1700(Nifty.java:77)
at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1374)
at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1329)
at com.jme3.niftygui.InputSystemJme.handleMouseEvent(InputSystemJme.java:124)
at com.jme3.niftygui.InputSystemJme.onMouseButtonEventQueued(InputSystemJme.java:197)
at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:261)
at de.lessvoid.nifty.Nifty.update(Nifty.java:288)
at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:113)
at com.jme3.input.InputManager.processQueue(InputManager.java:819)
at com.jme3.input.InputManager.update(InputManager.java:883)
at com.jme3.app.Application.update(Application.java:604)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:744)
BUILD SUCCESSFUL (total time: 6 seconds)
[/java]

And my code :

MyStartScreen.java :
[java]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.Properties;

public class MyStartScreen extends SimpleApplication implements Controller, ScreenController {

private Nifty nifty;
private NiftyJmeDisplay niftyDisplay;
private static MyStartScreen screen;
private int azerty;

public static void main(String[] args) {

  screen = new MyStartScreen();
      screen.start();
}

public void play() {
    System.out.println("1"+nifty+"             "+niftyDisplay);
    System.out.println(azerty);
    try {
        nifty.addXml("Interface/loading_screen.xml");
        nifty.gotoScreen("loading_layer");
    } catch(Exception e) {
        e.printStackTrace();
    }
    
}

public void quit() {
    screen.stop();
}

@Override
public void simpleInitApp() {
    
    System.out.println(azerty);
    niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
    System.out.println("2"+nifty+"             "+niftyDisplay);
    /** Create a new NiftyGUI object */
    nifty = niftyDisplay.getNifty();
    System.out.println("3"+nifty+"             "+niftyDisplay);
    /** Read your XML and initialize your custom ScreenController */
    System.out.println("4"+nifty+"             "+niftyDisplay);
    nifty.fromXml("Interface/start_menu.xml", "start");
    // nifty.fromXml("Interface/helloworld.xml", "start", new MySettingsScreen(data));
    // attach the Nifty display to the gui view port as a processor
    guiViewPort.addProcessor(niftyDisplay);
    System.out.println("5"+nifty+"             "+niftyDisplay);
    // disable the fly cam
    flyCam.setDragToRotate(true);
    System.out.println("6"+nifty+"             "+niftyDisplay);
    azerty = 1;
    System.out.println(azerty);
}

public void bind(Nifty nifty, Screen screen) {}

public void onStartScreen() {}

public void onEndScreen() {}

public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {}

public void init(Properties parameter, Attributes controlDefinitionAttributes) {}

public void onFocus(boolean getFocus) {}

public boolean inputEvent(NiftyInputEvent inputEvent) {return false;}

}[/java]

start_menu.xml :
|java]<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />

&lt;screen id="start" controller="mygame.MyStartScreen"&gt;
    &lt;layer id="layer" childLayout="center"&gt;
        &lt;panel id = "panel2" height="30%" width="50%" align="center" valign="center" childLayout="vertical"&gt;
            &lt;control name="button" label="Start" id="StartButton" align="center" valign="center" visibleToMouse="true"&gt;
                &lt;interact onClick="play()" /&gt;
            &lt;/control&gt;
            &lt;control name="button" label="Quit" id="QuitButton" align="center" valign="center" visibleToMouse="true"&gt;
                &lt;interact onClick="quit()" /&gt;
            &lt;/control&gt;
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>[/java]

loading_screen.xml :
[java]<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />

&lt;controlDefinition name="loadingbar" controller="mygame.MyStartScreen"&gt;
    &lt;image filename="Interface/progressbar/border.png" childLayout="absolute" imageMode="resize:15,2,15,15,15,2,15,2,15,2,15,15"&gt;
        &lt;image id="progressbar" x="0" y="0" filename="Interface/progressbar/inner.png" width="32px" height="100%" imageMode="resize:15,2,15,15,15,2,15,2,15,2,15,15" /&gt;
    &lt;/image&gt;
&lt;/controlDefinition&gt;

&lt;screen id="loading_layer" controller="mygame.MyStartScreen"&gt;
    &lt;layer id="loadinglayer" childLayout="center" backgroundColor="#0000"&gt;
        &lt;panel id="loadingpanel" childLayout="vertical" align="center" valign="center" height="32px" width="70%"&gt;
            &lt;control name="loadingbar" align="center" valign="center" width="100%" height="100%" /&gt;
            &lt;control id="loadingtext" name="label" align="center" text="                                                  "/&gt;
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>
[/java]

Greets,

anamani

Sorry for this double post but i’ve omitted to include the start_menu.xml, so it is.
[java]<?xml version=”1.0″ encoding=”UTF-8″?>
<nifty xmlns=”http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd“>
<useStyles filename=”nifty-default-styles.xml” />
<useControls filename=”nifty-default-controls.xml” />

&lt;screen id=”start” controller=”mygame.MyStartScreen”&gt;
    &lt;layer id=”layer” childLayout=”center”&gt;
        &lt;panel id = “panel2″ height=”30%” width=”50%” align=”center” valign=”center” childLayout=”vertical”&gt;
            &lt;control name=”button” label=”Start” id=”StartButton” align=”center” valign=”center” visibleToMouse=”true”&gt;
                &lt;interact onClick=”play()” /&gt;
            &lt;/control&gt;
            &lt;control name=”button” label=”Quit” id=”QuitButton” align=”center” valign=”center” visibleToMouse=”true”&gt;
                &lt;interact onClick=”quit()” /&gt;
            &lt;/control&gt;
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>[/java]

Oops, i’ve forgotten the following of this sentence too :
"When I verify the values of Nifty and NiftyDisplay (and a test int), they "… are null !

You’ve never registered your controller with nifty so it is creating its own instance… so of course all of the fields set during normal app initialization will be null.

1 Like

I thought I had already done it in the xml file : "controller=“mygame.MyStartScreen”.
It’s not that ?

Otherwise, how can I do this ? With that ?
[java]
nifty.addScreen(“start”, new ScreenBuilder(“start”) {{
controller(new mygame.MyStartScreen())}});[/java]

“controller=”mygame.MyStartScreen” tells Nifty which class is bound to the screen. It can’t just find an instance that’s been instantiated somewhere, and if it can’t find one when parsing the XML file, then it creates it’s own one with a default constructor via Reflection. So you need to pass your instance of that class to nifty.

The easiest way in that scenario is to register it at the same time as adding the XML, i.e:

[java]nifty.fromXml(“Interface/start_menu.xml”, “start”, this);[/java]

1 Like

Owwww !!! It works !!! Thank you very much !!!