[SOLVED] Trouble With findElementByName in MonkeyZone code

I’m very new to jme3 and 3d engines in general. I’ve also never used NiftyGUI in the past.

That said, I have been through the Beginner’s Tutorials and several of the intermediate ones here:

To advance my project further, I wanted to look at a small, simple, functional game program to see the pieces fit together. To that end, I found a repo for the MonkeyZone game, downloaded it, upgraded it to 3.1 and fixed all the errors.

Except for one error.

This error exists in 12 places in the project, across 3 class files.

Wherever the method screen.findElementByName(String name) is called, the first instance of that is crossed out, as though deprecated, with the following instance throwing an error.

Here are three examples:

Element panel = screen.findElementByName("layer").findElementByName("panel").findElementByName("players_panel").findElementByName("players_list").findElementByName("panel");
hitPoints = screen.findElementByName("layer").findElementByName("panel_bottom").findElementByName("bottom_panel_left").findElementByName("status_text_01").getRenderer(TextRenderer.class);
statusText = nifty.getScreen("load_game").findElementByName("layer").findElementByName("panel").findElementByName("status_text").getRenderer(TextRenderer.class);

The error thrown is “cannot find symbol.” As below:

C:\Users\Christopher\MyJMonkey\trunk\src\com\jme3\monkeyzone\ClientMain.java:190: error: cannot find symbol
    statusText = nifty.getScreen("load_game").findElementByName("layer").findElementByName("panel").findElementByName("status_text").getRenderer(TextRenderer.class);
symbol:   method findElementByName(String)
location: class Element

Hovering over the problem returns the following:

Dereferencing possible null pointer

Any help would be thoroughly appreciated. Please let me know if there is anything else I can add to help clarify the situation!

P.S. – Additionally, although not throwing a warning, one library is also crossed out as though deprecated:

import de.lessvoid.nifty.controls.textfield.TextFieldControl;

I am unsure if this is related.

This is not good Java, but I did create a workaround.

First, findElementByName(String name) is indeed deprecated, and the correct method is now findElementbyId(String name).

So, I changed all the methods, but was still receiving the dereferencing problems. This, I believe, became strongly enforced in Java 1.7. (The not good coding practices part): So, I enclosed all of my findElement… method calls in try/catch blocks. Viewing the ClientUI.xml file associated with MonkeyZone allowed me to confirm that definitely none of the 12 instances of this call would return null. For any of those calls that needed access to that data afterwards, I initialized the variable to null prior to the try/catch block. Since I knew it wouldn’t be null after getting through the block, I wasn’t worried about messing anything up down the line.

I did wind up with two final errors after finishing this.

There was a problem with a bullet library needing to be upgrading. That problem was resolved from this thread:

Additionally, there was a problem with updating the list of connected client names. You can no longer call getElements() on a single Element in order to create a LinkedList to iterate over. So, I commented out the updating of player names in the lobby. I’ll have to figure out some other way to dynamically update that information.

Ultimately, it works now, so – I’m going to mark this as Solved, even though there are definitely better coding solutions. At least now, my efforts in understanding a simple JME game as a whole can proceed.

Thanks to anyone who even took the time to give this thread a read. If you’ve still got a better solution, I’m still interested in hearing it!