BitmapText.setText not working

I have the following code:



[java]

BitmapText pressureValue;

BitmapFont font = new BitmapFont();

String pressureString = " ";[/java]



[java]

pressure = value * 100;

System.out.println("1 "+pressure);

pressureString = “”+pressure;

System.out.println("2 “+pressureString);

pressureValue.setText(” “+pressureString);

System.out.println(“3”);

pressureValue.setColor(ColorRGBA.White);

pressureValue.setLocalTranslation(100, 100, 10);

gui.attachChild(pressureValue);[/java]



When running, I am given the following exception:



SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException



This exception is at pressureValue.setText(” "+pressureString);



What is the problem?

pressureValue is null

I got that far. But even when I initializa it as following: [java]pressureValue.setText(" ");[/java], the same thing happens.

its null, it will only become not null if you assign an existing or new object ^^

[java]pressureValue = new BitmapText();[/java]

Gets underlined in red - can’t do it :confused:

Import?

Ah no, it needs a font in the constructor.

Here’s the whole class :stuck_out_tongue:

[java]/*

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

    */

    package mygame;



    import com.jme3.font.BitmapFont;

    import com.jme3.font.BitmapText;

    import com.jme3.input.InputManager;

    import com.jme3.input.MouseInput;

    import com.jme3.input.controls.AnalogListener;

    import com.jme3.input.controls.MouseButtonTrigger;

    import com.jme3.math.ColorRGBA;

    import com.jme3.scene.Node;



    /**

    *
  • @author Mamo

    */

    public class Controls{



    Node gui;

    float pressure;

    BitmapText pressureValue = new BitmapText();

    BitmapFont font = new BitmapFont();

    String pressureString = " ";



    public void getNode(Node guiNode){

    gui = guiNode;

    }



    public void controlsSetup(InputManager inputManager) {

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

    inputManager.addListener(analogListener, "LMB");

    }



    private AnalogListener analogListener = new AnalogListener() {

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

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

    pressure = value * 100;

    System.out.println("1 "+pressure);

    pressureString = ""+pressure;

    System.out.println("2 "+pressureString);

    pressureValue.setText(" "+pressureString);

    System.out.println("3");

    pressureValue.setColor(ColorRGBA.White);

    pressureValue.setLocalTranslation(100, 100, 10);

    gui.attachChild(pressureValue);

    }

    }

    };

    }

    [/java]

Yeah. http://hub.jmonkeyengine.org/javadoc/com/jme3/font/BitmapText.html

The SDK shows you the constructors if you press Ctrl-Space on the class name.

When I add the font, the following exception comes:



Exception in thread “main” java.lang.NullPointerException

at com.jme3.font.BitmapFont.getPageSize(BitmapFont.java:126)



What are Pages, and what must I add?

I think you need to instantiate the font with an image or something too, else where should the font come from?

Is there any simple way how to show Text using guiNode?

Sure :stuck_out_tongue:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:hud

[java]BitmapFont myFont = assetManager.loadFont(“Interface/Fonts/Console.fnt”);

hudText = new BitmapText(myFont, false);[/java]



Edit: btw, Ctrl-I and entering “BitmapText” in the input field gets you this info instantly.

Never mind, solved it by copying the stats :slight_smile: Thanks :smiley:

@memonick said:
When I add the font, the following exception comes:

Exception in thread "main" java.lang.NullPointerException
at com.jme3.font.BitmapFont.getPageSize(BitmapFont.java:126)

What are Pages, and what must I add?


I know you found your own workaround, but I ran into this same issue and figured I would share in case anyone else encounters it. It turned out to be the way I was exporting the .fnt file using AngelCode's Bitmap Font Generator (v1.12a). I got this same JME runtime error after fiddling with the export settings in BFG. Specifically, the error started with the XML and Binary options set active; the Text option worked fine.

In memonick’s case, he wasn’t even loading a font. So even though he had a BitmapFont object, it didn’t actually have a font in it thus threw a NullPointerException when trying to use it.