Hi everyone! I’m having an issue with nifty GUI textfield. In no way, can I retrieve the text from a text field. It throws a NullPointerException and it seems like it had to do with the text renderer. I’ve looked through this forum for a conclusion to my problem but nothing seems to help. Below is the full source file:
[java]
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
public class GraphicsInterface extends AbstractAppState implements ScreenController {
private Nifty nifty;
private Screen screen;
private SimpleApplication app;
private AssetManager assetManager;
private InputManager inputManager;
private AudioRenderer audioRenderer;
private ViewPort guiViewPort;
private NiftyJmeDisplay niftyDisplay;
private Element textField;
private Element textline;
public GraphicsInterface(SimpleApplication app) {
this.assetManager = app.getAssetManager();
this.inputManager = app.getInputManager();
this.audioRenderer = app.getAudioRenderer();
this.guiViewPort = app.getGuiViewPort();
}
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
}
public void onEndScreen() {
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication)app;
niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = niftyDisplay.getNifty();
nifty.fromXml("Interface/screen.xml", "hud", this);
textField = nifty.getCurrentScreen().findElementByName("textfield-text");
String text;
text = textField.getRenderer(TextRenderer.class).getOriginalText();
textline = nifty.getCurrentScreen().findElementByName("textline");
textline.getRenderer(TextRenderer.class).setText(text);
guiViewPort.addProcessor(niftyDisplay);
}
@Override
public void update(float tpf) {
}
@Override
public void stateAttached(AppStateManager stateManager) {
}
@Override
public void stateDetached(AppStateManager stateManager) {
}
}
[/java]
Below is my XML file:
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“https://raw.github.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“https://raw.github.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd https://raw.github.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd”>
<useStyles filename="nifty-default-styles.xml" />
<useStyles filename="Interface/style.xml" />
<useControls filename="nifty-default-controls.xml" />
<screen id="start">
</screen>
<screen id="hud">
<layer id="background">
</layer>
<layer id="foreground" childLayout="vertical">
<panel id="panel_top" width="100%" height="20%" childLayout="horizontal">
</panel>
<panel id="panel_middle" width="100%" height="73%" childLayout="center">
</panel>
<panel id="panel_below_middle" width="100%" height="3%" childLayout="center">
<text id="textline" text="Slender man is hunting you!" font="Interface/Fonts/Default.fnt"
width="100%" height="100%" color="#FFA500">
</text>
</panel>
<panel id="panel_bottom" width="100%" height="6%" childLayout="horizontal">
<control id="input" name="textfield" maxLength="100" text=""></control>
</panel>
</layer>
</screen>
</nifty>
[/java]
Surely there is something I’m doing wrong, I’m pretty new to jMonkey and Nifty. Any help will be appreciated. Thanks!