simpleUpdate with guiNode issue

Hello,



I am working on a project which uses JMonkey3 and I am having problems with simpleUpdate. To understand exactly the problem have cut the majority of my larger application out, leaving only the core methods where I believe the problem lies.



Class: Avatar

JMonkey class which extends SimpleApplication and implements AnimEventListener. A very simple HUD is created using guiNode (not Nifty), displaying BitMap text onscreen. SimpleUpdate should update each BitMap text using specified strings.



simpleUpdate

[java]

public void simpleUpdate(float tpf)

{

one.setText(oneS);

two.setText(twoS);

three.setText(threeS);

four.setText(fourS);

five.setText(fiveS);

}

[/java]



The 5 BitMap texts and 5 String values are private class variables:



[java]

private BitmapFont fnt;

private BitmapText one, two, three, four, five;

private String oneS, twoS, threeS, fourS, fiveS;

[/java]



Using an actionListener I can change the values of the strings (oneS, twoS etc) if I perform a key press. As expected simpleUpdate sets the BitMap text to be the new string value every time… YAY. Problem is this is not how I want it to work…



The Avatar class contains a method called “updateText(ArrayList myStrings)”. The class Conversation is a NONE Jmonkey3 based class. This class at one point creates an Avatar object and passes an ArrayList to the Avatar objects updateText. updateText assigns oneS, twoS, threeS (etc) to equal the passed ArrayList’s strings. I assumed that once updateText had been run, the private class String variables would change and therefore simpleUpdate would work as it did with the actionListener. Instead once updateText has been executed the private class String variables aren’t changed, thus no update occurs. If I perform a simple System.out.println statement within the updateText() method I am told it has been assigned correctly, therefore I don’t understand why this isn’t working. What have I done wrong here?



[java]

public void updateText(ArrayList<String> myString)

{

oneS = myString.get(0);

twoS = myString.get(1);

threeS = myString.get(2);

fourS = myString.get(3);

fiveS = myString.get(4);

}

[/java]



Hope you can help! This is driving me mad.



Mark

You have to use the setText method in the updateText method, you are just replacing the pointer to the String object, you have to set the new String to the BitmapText.

I did exactly what you suggested earlier this afternoon, before I made my original post. If I do that I receive a NullPointerException error as soon as it tries to assign oneS as text to one (as an example).



[java]

public void updateText(ArrayList<String> myString)

{

System.out.println("");

oneS = myString.get(0);

twoS = myString.get(1);

threeS = myString.get(2);

fourS = myString.get(3);

fiveS = myString.get(4);



one.setText(oneS);

two.setText(twoS);

three.setText(threeS);

four.setText(fourS);

five.setText(fiveS);



System.out.println("
");

}

[/java]



Error:

09-Feb-2011 22:40:12 com.jme3.app.Application handleError

SEVERE: Uncaught exception thrown in Thread[AWT-EventQueue-0,6,main]

java.lang.NullPointerException

at prototypeV3.Avatar.updateText(Avatar.java:283)

at prototypeV3.ConversationController.runNode(ConversationController.java:91)

well this means “one” is null when you get there.

You have an init problem, when do you instantiate “one”?

I agree. For some reason it thinks one is null. I don’t understand why though. My BitmapTexts are initialised by a method called gui() when simpleInitApp is called at the beginning of the program. When I initialise one, two ect in gui() they should be stored in the private class variables at the top of the class right? I have included more code below to make the problem easier to troubleshoot (note: I have taken out various methods which are unrelated to this problem).



Many thanks in advance.



Mark!



[java]

public class Avatar extends SimpleApplication implements AnimEventListener {



private AnimChannel channel;

private AnimControl control;

private Node player;



private BitmapFont fnt;

private BitmapText one, two, three, four, five;

private String oneS, twoS, threeS, fourS, fiveS;



//Initialise Scene

@Override

public void simpleInitApp() {



viewPort.setBackgroundColor(ColorRGBA.White);

initKeys();



DirectionalLight dl = new DirectionalLight();

dl.setDirection(new Vector3f(-0.1f, -1f, -3).normalizeLocal());

rootNode.addLight(dl);



player = (Node) assetManager.loadModel(“assets/Models/robotMesh.mesh.xml”);

player.setLocalScale(0.5f);

player.rotate(-1.5f, 0.0f, 0.0f);

rootNode.attachChild(player);



//Load GUI

gui();



control = player.getControl(AnimControl.class);

control.addListener(this);

channel = control.createChannel();

channel.setAnim(“Start”);

}



public void gui()

{

//Remove Default scene graph statistics

guiNode.detachAllChildren();



//Create Window

Picture window = new Picture(“Window”);

window.setImage(assetManager, “assets/Textures/WindowV5.png”, true);

window.setWidth(settings.getWidth());

window.setHeight(400);

window.setPosition(0, 0);

guiNode.attachChild(window);



//Create Back Symbol

Picture back = new Picture(“Back”);

back.setImage(assetManager, “assets/Textures/Back.png”, true);

back.setWidth(150);

back.setHeight(150);

back.setPosition(1450, 750);

guiNode.attachChild(back);



//Initialise Strings

oneS = “1”; twoS = “2”; threeS = “3”; fourS = “4”; fiveS = “5”;



//Responses

fnt = assetManager.loadFont(“assets/Fonts/Arial.fnt”);



one = new BitmapText(fnt, false);

one.setColor(ColorRGBA.Black);

one.setText(oneS);

one.setLocalTranslation(80, 230, 0);

guiNode.attachChild(one);



two = new BitmapText(fnt, false);

two.setColor(ColorRGBA.Black);

two.setText(twoS);

two.setLocalTranslation(80, 190, 0);

guiNode.attachChild(two);



three = new BitmapText(fnt, false);

three.setColor(ColorRGBA.Black);

three.setText(threeS);

three.setLocalTranslation(80, 150, 0);

guiNode.attachChild(three);



four = new BitmapText(fnt, false);

four.setColor(ColorRGBA.Black);

four.setText(fourS);

four.setLocalTranslation(80, 110, 0);

guiNode.attachChild(four);



five = new BitmapText(fnt, false);

five.setColor(ColorRGBA.Black);

five.setText(fiveS);

five.setLocalTranslation(80, 70, 0);

guiNode.attachChild(five);

}





public void updateText(ArrayList<String> myString)

{

System.out.println(“");

oneS = myString.get(0);

twoS = myString.get(1);

threeS = myString.get(2);

fourS = myString.get(3);

fiveS = myString.get(4);



one.setText(oneS);

two.setText(twoS);

three.setText(threeS);

four.setText(fourS);

five.setText(fiveS);



System.out.println("
”);

}

public void simpleUpdate(float tpf)

{

this.one.setText(oneS);

two.setText(twoS);

three.setText(threeS);

four.setText(fourS);

five.setText(fiveS);

}

[/java]