NullPointer driving me up the wall.

So, I’m getting a null-pointer while trying to draw a TextField using LibGDX… and I can’t for the life of me figure out why.

public void draw(SpriteBatch batch, float x, float y, float width, float height)
	{
		batch.draw(chatboxTexture, x, y, width, height);
		try 
		{
			userInput.draw(batch, 1.0f);
		}
		catch(NullPointerException npe)
		{
			npe.printStackTrace();
		}
	}

The strange part is, that the TextField still draws, but it’s not usable (Can’t input text on it, etc)

Here’s the ChatBox constructor, which sets chatBoxTexture aswell

	public Chatbox(Texture texture, int width, int height)
	{

		this.chatboxTexture = texture;
		this.width = width;
		this.height = height;
		this.isInputActive = false;
		this.message = "";
		this.font = new BitmapFont();
		this.textFieldStyle = new TextFieldStyle();
		this.skin = new Skin();
		this.textureAtlas = new TextureAtlas(Constants.CACHE_DIRECTORY + "/data/gui/packed/Interfaces.pack");
		this.skin.addRegions(textureAtlas);
		this.textFieldStyle.background = skin.getDrawable("chat_input");
		this.textFieldStyle.font = font;
		this.userInput = new TextField("Test", textFieldStyle);
		this.userInput.setTextFieldListener(new TextFieldListener() 
		{
			@Override
			public void keyTyped(TextField textField, char key)
			{
				System.out.println("[Debug] Key Pressed: " + key);
			}
		});
		this.userInput.setMessageText("Test");
	}

So, the chatbox, and the text-field is drawn, however. It’s throwing nulls out the wazoo and I can’t figure out why.

Go through and make sure every single one of your objects are initialized. Sorry, but the compiler doesn’t lie, something hasn’t been created yet. I would bet the spritebatch.

Can we have a stacktrace?

I doubt it would draw itself without the spritebatch, anyway

  spriteBatch.begin();
        dataText.draw(spriteBatch, "Mouse X: " + Gdx.input.getX() + "\n Mouse Y: " + Gdx.input.getY() +"\n Player(X: " + player.getXPosition() + " Y: " + player.getYPosition() + " )", 
        		 0 ,  600);
        chatbox.draw(spriteBatch, 0 , 0, 449, 139); 
        spriteBatch.end();/code]


The spritebatch is there.





java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.ui.TextField.draw(TextField.java:514)
at com.pokefrontier.graphic.component.Chatbox.draw(Chatbox.java:79)
at com.pokefrontier.state.InGame.render(InGame.java:100)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.pokefrontier.Game.render(Game.java:50)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)





Line 100 -  InGame.java

chatbox.draw(spriteBatch, 0 , 0, 449, 139);



Line 79: Chatbox.java

userInput.draw(batch, 1.0f);



Is the font null?

EDIT: Or the font colour.

Got this fixed, thanks for the input Heroes. The FontColour was not being read by the Bitmap file.