I tried to use the code from the documentation, but encountered a problem: an instance of the Nifty class is not available from the screen controller. Tried to call it like so:
Main.java
public class Main extends SimpleApplication {
...
public static Nifty nifty;
public static Client myClient;
...
}
LoginScreen.java
public class LoginScreen extends BaseAppState implements ScreenController {
...
public void GoToSignUp() {
NiftyEMailText String = Main.nifty.getCurrentScreen().findElementById("EMail").getRenderer(TextRenderer.class).getWrappedText();
NiftyPassWordText String = Main.nifty.getCurrentScreen().findElementById("PassWord").getRenderer(TextRenderer.class).getWrappedText();
Main.nifty.gotoScreen("SignUpScreen");
Element niftyEMail = Main.nifty.getCurrentScreen().findElementById("EMail");
niftyEMail.getRenderer(TextRenderer.class).setText(niftyEMailText);
Element niftyPassWord = Main.nifty.getCurrentScreen().findElementById("PassWord");
niftyPassWord.getRenderer(TextRenderer.class).setText(niftyPassWordText);
}
...
}
screen.xml
<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.lessvoid.com/nifty-gui" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd
https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd">
<useStyles filename="nifty-default-styles.xml"/>
<useControls filename="nifty-default-controls.xml"/>
<screen id="start" controller="com.yasen.client.LoginScreen">
<layer id="background" childLayout="center">
<image filename="Interface/start-background.jpg"></image>
</layer>
<layer id="foreground" childLayout="center">
<panel id="panel" height="25%" width="75%" align="center" childLayout="horizontal">
<panel id="panel_left" height="100%" width="25%" align="left" childLayout="vertical">
<text text="E-mail:" font="Interface/Fonts/Default.fnt"/>
<text text="Password:" font="Interface/Fonts/Default.fnt"/>
</panel>
<panel id="panel_center" height="100%" width="50%" align="center" childLayout="vertical">
<control id="EMail" name="textfield" maxLength="50" font="Interface/Fonts/Default.fnt"/>
<control id="PassWord" name="textfield" maxLength="100" passwordChar="*" font="Interface/Fonts/Default.fnt"/>
</panel>
<panel id="panel_right" height="100%" width="25%" align="right" childLayout="vertical">
<control id="SignUp" name="button" label="Register" font="Interface/Fonts/Default.fnt" visibleToMouse="true">
<interact onClick="GoToSignUp()"/>
</control>
<control id="LogIn" name="button" label="Login" font="Interface/Fonts/Default.fnt" visibleToMouse="true">
<interact onClick="AuthReq()"/>
</control>
</panel>
</panel>
</layer>
</screen>
<screen id="SignUpScreen" controller="com.yasen.client.LoginScreen">
<layer id="background" childLayout="center">
<image filename="Interface/start-background.jpg"></image>
</layer>
<layer id="foreground" childLayout="center">
<panel id="panel" height="25%" width="75%" align="center" childLayout="horizontal">
<panel id="panel_left" height="100%" width="25%" align="left" childLayout="vertical">
<text text="E-mail:" font="Interface/Fonts/Default.fnt"/>
<text text="Password:" font="Interface/Fonts/Default.fnt"/>
</panel>
<panel id="panel_center" height="100%" width="50%" align="center" childLayout="vertical">
<control id="EMail" name="textfield" maxLength="50" font="Interface/Fonts/Default.fnt"/>
<control id="PassWord" name="textfield" maxLength="100" passwordChar="*" font="Interface/Fonts/Default.fnt"/>
<control id="PassWord2" name="textfield" maxLength="100" passwordChar="*" font="Interface/Fonts/Default.fnt"/>
</panel>
<panel id="panel_right" height="100%" width="25%" align="right" childLayout="vertical">
<control id="SignUp" name="button" label="Sign Up" font="Interface/Fonts/Default.fnt" visibleToMouse="true">
<interact onClick="SignUpReq()"/>
</control>
</panel>
</panel>
</layer>
</screen>
</nifty>
But got an error:
Caused by: java.lang.NullPointerException
at com.yasen.client.LoginScreen.GoToSignUp(LoginScreen.java:53)
As I understand it, meaning that the program is trying to access a non-existent GUI node.
How to correctly pass an instance of the Nifty class to the screen controller to work?