Nifty Object Null After SimpleInit()

My Nifty object becomes null after it is initialized. Cannot create popups because of this. Not sure what the issue is?



[xml]

<?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">

<!-- +++++++++++++++++++++++++++++++++++++++ -->

<!-- start screen -->

<!-- +++++++++++++++++++++++++++++++++++++++ -->

<screen id="start" controller="mygame.editor.MapEditor">

<layer id="background" childLayout="horizontal">

<panel width="80%" height="100%" childLayout="vertical">

</panel>

<panel padding="10px" width="20%" height="100%" childLayout="vertical" backgroundColor="#bbbb6666">



<control id="btnEmpty" name="button" label="Empty" >

<interact onClick="setBlockType(0)"/>

</control>

<control id="btnGrass" name="button" label="Grass" >

<interact onClick="setBlockType(1)"/>

</control>

<control name="button" label="Dirt" >

<interact onClick="setBlockType(3)"/>

</control>

<control id="btnRock" name="button" label="Rock" >

<interact onClick="setBlockType(4)"/>

</control>

<control id="btnStone" name="button" label="Stone" >

<interact onClick="setBlockType(2)"/>

</control>

<control id="btnWood" name="button" label="Wood" >

<interact onClick="setBlockType(5)"/>

</control>

<control id="btnTree" name="button" label="Tree" >

<interact onClick="setEntityType(Tree)"/>

</control>

<text color="#ffff" font="aurulent-sans-16.fnt"

text="Brush Size" />

<control id="txtBrushSize" name="textfield" text="0">

</control>

<control id="btnShowSave" name="button" label="Save">

<interact onClick="showSave()"/>

</control>

</panel>

</layer>

</screen>

<popup id="popupSave" childLayout="center" backgroundColor="#000a">

<panel padding="25px" width="300px" height="200px" childLayout="vertical" style="nifty-panel">

<text color="#ffff" font="aurulent-sans-16.fnt"

text="FileName" align="center" />

<control id="input" name="textfield" width="200px" align="center" />



<panel padding="10px" childLayout="horizontal">

<panel childLayout="center" align="center" padding="8">

<control id="btnConnect" height="30px" name="button" align="center" valign="center" label="Save">

<interact onClick="saveMap()" />

</control>

</panel>

<panel childLayout="center" align="center" padding="8">

<control id="btnCancelConnect" height="30px" name="button" align="center" valign="center" label="Cancel">

<interact onClick="cancelSave()" />

</control>

</panel>

</panel>

</panel>

</popup>

</nifty>

[/xml]



[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package mygame.editor;



    import com.jme3.app.SimpleApplication;

    import com.jme3.bullet.BulletAppState;

    import com.jme3.input.KeyInput;

    import com.jme3.input.MouseInput;

    import com.jme3.input.controls.ActionListener;

    import com.jme3.input.controls.AnalogListener;

    import com.jme3.input.controls.KeyTrigger;

    import com.jme3.input.controls.MouseButtonTrigger;

    import com.jme3.light.AmbientLight;

    import com.jme3.light.DirectionalLight;

    import com.jme3.material.Material;

    import com.jme3.math.ColorRGBA;

    import com.jme3.math.Vector2f;

    import com.jme3.math.Vector3f;

    import com.jme3.niftygui.NiftyJmeDisplay;

    import com.jme3.scene.Geometry;

    import com.jme3.scene.shape.Box;

    import com.jme3.system.AppSettings;

    import com.jme3.texture.Texture;

    import com.jme3.texture.Texture.WrapMode;

    import de.lessvoid.nifty.Nifty;

    import de.lessvoid.nifty.controls.TextField;

    import de.lessvoid.nifty.elements.Element;

    import de.lessvoid.nifty.screen.Screen;

    import de.lessvoid.nifty.screen.ScreenController;

    import java.util.logging.Level;

    import java.util.logging.Logger;

    import mygame.entity.Entity;

    import mygame.entity.EntityManager;

    import mygame.entity.Tree;

    import mygame.main.SharedParts;

    import mygame.map.Block;

    import mygame.map.Map;

    import org.lwjgl.Sys;



    /**

    *
  • @author bgilb

    */

    public class MapEditor extends SimpleApplication implements ScreenController {



    public NiftyJmeDisplay niftyDisplay;

    public Nifty nifty;

    public Element popupSave;



    Map map;

    SharedParts sharedParts = new SharedParts(false);

    BulletAppState bullet;

    MapTileSelector tileSelector;

    boolean lookMode = true;

    long brushTime = Sys.getTime();

    int brushMilli = 100;

    int brushRadius = 4;

    TextField txtBrushSize;

    //blocks = 0; entities = 1

    static int drawMode = 0;

    static byte blockType = 0;

    static String entityType = "";

    EntityManager entityManager = new EntityManager();



    public static void main(String args[]) {

    AppSettings settings = new AppSettings(true);

    settings.setFrameRate(60);

    MapEditor mapEditor = new MapEditor();

    mapEditor.setSettings(settings);

    mapEditor.start(); // headless type for servers!

    }



    public void showSave(){

    popupSave = nifty.createPopup("popupSave");

    nifty.showPopup(nifty.getCurrentScreen(), popupSave.getId(), null);

    System.out.println(popupSave.getId());

    }



    public void saveMap(){



    }



    public void cancelSave(){



    }



    @Override

    public void simpleUpdate(float tpf) {

    tileSelector.update(!lookMode);

    }



    @Override

    public void simpleInitApp() {



    bullet = new BulletAppState();

    stateManager.attach(bullet);



    sharedParts.setApp(this);

    sharedParts.setAssetManager(assetManager);

    sharedParts.setInputManager(inputManager);

    sharedParts.setBulletPhysics(bullet);



    map = new Map(sharedParts, "assets/Textures/map1.raw");

    map.loadMap();



    flyCam.setMoveSpeed(200);



    AmbientLight al = new AmbientLight();

    al.setColor(ColorRGBA.White.mult(2.1f));

    rootNode.addLight(al);



    DirectionalLight sun = new DirectionalLight();

    sun.setColor(new ColorRGBA(0.8f, 0.8f, 0.85f, 1f));

    sun.setDirection(new Vector3f(-.5f, -.5f, -.5f).normalizeLocal());

    rootNode.addLight(sun);



    rootNode.attachChild(map);







    tileSelector = new MapTileSelector(sharedParts, map);

    rootNode.attachChild(tileSelector.getTileIndicator());



    setupEditorPlane();

    setKeys();

    setupGui();



    Logger.getLogger("de.lessvoid.nifty").setLevel(Level.SEVERE);

    Logger.getLogger("NiftyInputEventHandlingLog").setLevel(Level.SEVERE);

    }



    public void setupEditorPlane() {

    Geometry g1 = new Geometry();

    Box b = new Box(Vector3f.ZERO, 256, 0.1f, 256);

    b.scaleTextureCoordinates(new Vector2f(256, 256));

    g1.setMesh(b);



    Material mat1 = new Material(sharedParts.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    Texture t1 = sharedParts.assetManager.loadTexture("Textures/plane.png");

    t1.setWrap(WrapMode.Repeat);

    mat1.setTexture("ColorMap", t1);



    g1.setMaterial(mat1);

    g1.setLocalTranslation(256, -0.1f, 256);



    rootNode.attachChild(g1);

    }



    public void setupGui() {

    niftyDisplay = new NiftyJmeDisplay(

    assetManager, inputManager, audioRenderer, guiViewPort);

    nifty = niftyDisplay.getNifty();

    nifty.loadStyleFile("Interface/nifty-custom-style/nifty-custom-styles.xml");

    nifty.loadControlFile("nifty-default-controls.xml");

    nifty.fromXml("Interface/MapEditor.xml", "start");

    txtBrushSize = nifty.getCurrentScreen().findNiftyControl("txtBrushSize", TextField.class);



    guiViewPort.addProcessor(niftyDisplay);



    }



    public void setBlockType(String type) {

    drawMode = 0;

    blockType = (byte) Integer.parseInt(type);

    }



    public void setEntityType(String type) {

    drawMode = 1;

    entityType = type;

    }



    public void setKeys() {

    inputManager.addMapping("Toggle", new KeyTrigger(KeyInput.KEY_LCONTROL));

    inputManager.addMapping("Toggle", new KeyTrigger(KeyInput.KEY_RCONTROL));



    inputManager.addListener(actionListener, new String[]{"Toggle"});



    inputManager.addMapping("Remove", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));

    inputManager.addMapping("Add", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));



    inputManager.addListener(analogListener, new String[]{"Remove", "Add"});

    }



    public void enableMouseMode() {

    inputManager.setCursorVisible(true);

    flyCam.setEnabled(false);

    }



    public void enableLookMode() {

    inputManager.setCursorVisible(false);

    flyCam.setEnabled(true);

    }

    private ActionListener actionListener = new ActionListener() {



    public void onAction(String name, boolean keyPressed, float tpf) {

    if (name.equals("Toggle") && !keyPressed) {

    if (lookMode == true) {

    lookMode = false;

    enableMouseMode();

    } else {

    lookMode = true;

    enableLookMode();

    }

    }

    }

    };

    private AnalogListener analogListener = new AnalogListener() {



    public void onAnalog(String name, float keyPressed, float tpf) {



    if (name.equals("Add")) {



    if (Sys.getTime() - brushTime >= brushMilli) {



    brushRadius = Integer.parseInt(txtBrushSize.getText());

    Vector3f targetBlock = tileSelector.getTargetBlock().clone();

    targetBlock.addLocal(tileSelector.contactNormal);



    if (drawMode == 0) {

    map.setBlock((int) targetBlock.x, (int) targetBlock.z, (int) targetBlock.y, blockType, true);



    for (int x = -brushRadius; x <= brushRadius; x++) {

    for (int z = -brushRadius; z <= brushRadius; z++) {

    for (int y = -brushRadius; y <= brushRadius; y++) {

    Vector3f targetBlock2 = new Vector3f(x + targetBlock.x, y + targetBlock.y, z + targetBlock.z);

    float dist = targetBlock.distance(targetBlock2);

    if (targetBlock.distance(targetBlock2) < brushRadius) {

    map.setBlock((int) targetBlock2.x, (int) targetBlock2.z, (int) targetBlock2.y, blockType, false);

    }

    }

    }

    }

    map.rebuild();

    } else if (drawMode == 1) {

    if (entityType.equals("Tree")) {

    if (entityManager.getEntityOnTile(targetBlock) == null) {

    Tree t = new Tree(sharedParts);

    t.setTilePosition(targetBlock);

    rootNode.attachChild(t);

    entityManager.add(t);

    } else {

    System.out.println("Entity already there");

    }

    }

    }

    brushTime = Sys.getTime();

    }

    }





    if (name.equals("Remove")) {

    System.out.println("Remove!!");

    if (Sys.getTime() - brushTime >= brushMilli) {

    brushRadius = Integer.parseInt(txtBrushSize.getText());



    Vector3f targetBlock = tileSelector.getTargetBlock();

    if (drawMode == 0) {

    map.setBlock((int) targetBlock.x, (int) targetBlock.z, (int) targetBlock.y, Block.EMPTY_BLOCK, true);



    for (int x = -brushRadius; x <= brushRadius; x++) {

    for (int z = -brushRadius; z <= brushRadius; z++) {

    for (int y = -brushRadius; y <= brushRadius; y++) {

    Vector3f targetBlock2 = new Vector3f(x + targetBlock.x, y + targetBlock.y, z + targetBlock.z);

    float dist = targetBlock.distance(targetBlock2);

    if (targetBlock.distance(targetBlock2) < brushRadius) {

    map.setBlock((int) targetBlock2.x, (int) targetBlock2.z, (int) targetBlock2.y, Block.EMPTY_BLOCK, false);

    }

    }

    }

    }

    map.rebuild();

    }else if(drawMode ==1){

    targetBlock.y = targetBlock.y +1;

    Entity e1 = entityManager.getEntityOnTile(targetBlock);

    if(e1 != null){

    rootNode.detachChild(e1);

    entityManager.remove(e1);

    }

    }

    brushTime = Sys.getTime();

    }

    }

    }

    };

    }

    [/java]





    showSave() is where it goes null.

Where is the error that you are getting?

Also make your nifty object private and see what else could be setting it. From the looks of your code it is only set in setupGui()

where are your bind/startscreen/endscreen methods?

I removed them when pasting because they were empty. Sorry about that.



I don’t get any error, basically nothing happens when I hit the show save button. The only reason I know its null is because I set a breakpoint and it says null.

Okay i surrounded it with a try catch and got this:



Problem making popup java.lang.NullPointerException



[java]

public void showSave(){

try{

popupSave = nifty.createPopup("popupSave");

}catch(Exception e){

System.out.println("Problem making popup " + e);

}

nifty.showPopup(nifty.getCurrentScreen(), popupSave.getId(), null);

System.out.println(popupSave.getId());

}

[/java]

Is it possible for a object to reinitialize itself? I noticed these local variables kept reinitalizing themselves. It was very weird, but went away when I made them static:

static int drawMode = 0;

static byte blockType = 0;

static String entityType = “”;

I think this is what happens:

Your application starts, eventually it will come to this line

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



Nifty will read the XML-file and see



[xml]<screen id=“start” controller=“mygame.editor.MapEditor”>[/xml]



Which will make Nifty create an instance of the screencontroller. Which is

[java]public class MapEditor extends SimpleApplication implements ScreenController [/java]



So either you make nifty aware that you already have an instance of the controller by using registerController (I think it’s called) or better yet: make a separate class that is a screencontroller.

After much hassle around using Nifty GUI I found out that by making your ScreenControllers separate classes you will save you a lot of trouble. I don’t think the advantages of extending some other classes and implementing ScreenController on top of that is worth it.