Failing to findNiftyControl (using builder) (solved)

Ok, I just recently started converting my simple chat box into builder style rather than xml.



What I’m doing is creating the screens then attaching different layers for different parts of the HUD/GUI.



So for the chat this is what I’m doing to construct the layer.

(this is during initialization of the chat appstate)



[java] private Element buildChat() {

return new LayerBuilder(“chat”) {

{

controller(new ChatController());

childLayoutCenter();

panel(new PanelBuilder(“chatPanel”) {

{

childLayoutVertical();

width(“600”);

height(“150”);

alignLeft();

valignBottom();

visibleToMouse(true);

backgroundColor("#00000055");

padding(“10”);

panel(new PanelBuilder(“chatArea”) {

{

childLayoutVertical();

height(“80%”);

alignLeft();

valignTop();

for (int i = 0; i < CHAT_LINES; i++) {

control(new LabelBuilder(“chat_line” + i) {

{

width(“100%”);

height(“14”);

alignLeft();

valignBottom();

}

});

}

}

});

panel(new PanelBuilder(“messageArea”) {

{

childLayoutHorizontal();

width(“100%”);

valignBottom();

control(new TextFieldBuilder(“text_input”) {

{

color("#444444EE");

}

});

control(new ButtonBuilder(“send_button”) {

{

text(“Send”);

interactOnClick(“sendMessage()”);

}

});

}

});

}

});

}

}.build(nifty, screen, screen.getRootElement());

}[/java]



Later when the app state is attached to the stateManager,

I’m first printing out the elements available in the actual screen, and then tries to map

the elements.



This used to work fine when I was using xml, but somehow I’m now getting null objects when

trying to find the nifty controls.



[java] private void init() {

printElements(screen.getRootElement(), “”);



for (int i = 0; i < CHAT_LINES; i++) {

chatLines = new ChatLine(screen.findNiftyControl(“chat_line” + i, Label.class), Color.WHITE);

}



button = screen.findNiftyControl(“send_button”, Button.class);

button.setFocusable(false);

textInput = screen.findNiftyControl(“text_input”, TextField.class);

textInput.setEnabled(false);

textInput.getElement().addInputHandler(new KeyInputHandler() {

@Override

public boolean keyEvent(NiftyInputEvent inputEvent) {

if (inputEvent == null)

return false;

if (inputEvent.getCharacter() == NiftyInputEvent.SubmitText.getCharacter()) {

if (textInput.hasFocus() && textInput.getText() != null) {

sendMessage();

textInput.setEnabled(false);

}

return true;

}

return false;

}

});

}[/java]



For this piece of code:



[java] button = screen.findNiftyControl(“send_button”, Button.class);

button.setFocusable(false);[/java]



I’m getting the error beneath



[java]chat[Element]

chatPanel[Element]

chatArea[Element]

chat_line0[Element]

chat_line1[Element]

chat_line2[Element]

chat_line3[Element]

chat_line4[Element]

chat_line5[Element]

chat_line6[Element]

messageArea[Element]

text_input[Element]

text_input#field[Element]

text_input#field#text[Element]

null[Element]

text_input#cursor[Element]

send_button[Element]

send_button#text[Element]

java.lang.NullPointerException

at de.lessvoid.nifty.controls.AbstractController.setFocusable(AbstractController.java:98)

at com.divicus.client.interfaces.chat.ChatController.init(ChatController.java:84)

at com.divicus.client.interfaces.chat.ChatController.stateAttached(ChatController.java:70)

at com.jme3.app.state.AppStateManager.attach(AppStateManager.java:127)

at com.divicus.client.appstates.GUIState.stateAttached(GUIState.java:42)

at com.jme3.app.state.AppStateManager.attach(AppStateManager.java:127)

at com.divicus.client.appstates.GameState.stateAttached(GameState.java:265)

at com.jme3.app.state.AppStateManager.attach(AppStateManager.java:127)

at com.divicus.client.ClientApplication.checkStatus(ClientApplication.java:153)

at com.divicus.client.ClientApplication.simpleUpdate(ClientApplication.java:145)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:241)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:184)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)

at java.lang.Thread.run(Unknown Source)[/java]



Has anyone experienced this or can anyone see a flaw in my code.

I’ve stared myself blind on this for the past hour or so.

I very rarely use the builder so the help I can propose is fairly limited, but I know you can use id(“myID”); so maybe re-setting it would work when you set text and interaction?



[java]

control(new ButtonBuilder(“send_button”) {

{

id(“send_button”); // add this. Maybe that’ll work?

text(“Send”);

interactOnClick(“sendMessage()”);

}

});

[/java]

1 Like

Alright I’ll give it a try.



Edit: Nope, buhu…

Hmm strange, seems like in the builder the controls are identified by name.

This solved it. Thanks for the reply though, got me looking for the identification methods.

[java] final String btnid = "send_button";

control(new ButtonBuilder(btnid) {

{

id(btnid);

name(btnid);

text("Send");

interactOnClick("sendMessage()");

}

});[/java]

1 Like

Hmm. That’s weird. But then again, Nifty has this bad habit of throwing curve balls at you when you least expect it.



Glad you found a way.