Getting text for textfield

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”>

&lt;useStyles filename="nifty-default-styles.xml" /&gt;
&lt;useStyles filename="Interface/style.xml" /&gt;
&lt;useControls filename="nifty-default-controls.xml" /&gt;

&lt;screen id="start"&gt;
    
&lt;/screen&gt;
&lt;screen id="hud"&gt;

    &lt;layer id="background"&gt;
        
    &lt;/layer&gt;
    &lt;layer id="foreground" childLayout="vertical"&gt;
        &lt;panel id="panel_top" width="100%" height="20%" childLayout="horizontal"&gt;
            
        &lt;/panel&gt;
        
        &lt;panel id="panel_middle" width="100%" height="73%" childLayout="center"&gt;
            
        &lt;/panel&gt;
        
        &lt;panel id="panel_below_middle" width="100%" height="3%" childLayout="center"&gt;
            &lt;text id="textline" text="Slender man is hunting you!" font="Interface/Fonts/Default.fnt" 
                  width="100%" height="100%" color="#FFA500"&gt;
            &lt;/text&gt;
        &lt;/panel&gt;
        
        &lt;panel id="panel_bottom" width="100%" height="6%" childLayout="horizontal"&gt;
            &lt;control id="input" name="textfield" maxLength="100" text=""&gt;&lt;/control&gt;
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>
[/java]

Surely there is something I’m doing wrong, I’m pretty new to jMonkey and Nifty. Any help will be appreciated. Thanks!

Sorry, please replace this:

[java]
textField = nifty.getCurrentScreen().findElementByName(“textfield-text”);
[/java]

To this:
[java]
textField = nifty.getCurrentScreen().findElementByName(“input”);
[/java]

The second one is the original one, not the above one. Still having the same issues.

[java]
TextField input = nifty.getCurrentScreen().findNiftyControl(“input”, TextField.class);
input.getRealText()
[/java]

That’s how I retrieve the controls text. I set it by using the renderer like you have it though.

1 Like

Thanks! What is the import for ‘TextField’? I always try to find it but some way it doesn’t find it.

Never mind, I found the import and the code is working perfectly. Thanks man!