JavaFX: How do I access the methods of an object owned by a pane?

I’m new to JFX, so this question probably has an obvious answer.

When I started my game I was following a tutorial. The source is at https://github.com/AlmasB/FXTutorials/tree/master/src/com/almasb/asteroids.

The developer who wrote it had different objects within the game represented by private static classes declared in the main Java file, each of which extended a single broad “Game Object” class written in another file. When I needed a new method or member variable I added it to this Game Object class to avoid cluttering the inline class declarations in the main file. Eventually I decided that that didn’t make sense and it shouldn’t be the case that every single game object has various data and methods it will never use. So I started splitting these private classes out into their own files and giving each only what it needed.

As a result, in many of my function calls to various objects, I can’t access those methods. This wasn’t a problem when these classes were private static classes of the Application.

For example:

Text currentHealth = new Text(0, 20,"Health: " + player.getHealth());
        dataPane.getChildren().set(0, currentHealth);

In this case the IDE lets me attempt to run the program, but when it launches there’s a null pointer exception at that first line.

I tried something like:

actionPane.getChildren().get(0).somePlayerClassMethod();

It also doesn’t work. Though index 0 of that list definitely is the player object, I can’t access any methods related to the Player or Game Object classes (player is both).

I’m just not clear on why moving these classes into their own files made it difficult to access their instantiations’ methods. Each of them is a child of a Pane, which may or may not be relevant.

Again, the only thing that seems to have changed is that these classes are no longer private static classes within the Application class. Any ideas?

Edit: fixed typos.

I can’t figure it out from your description. I’m going to make a guess this has more to do with issues pertaining to the different forms of classes (static inner classes versus using a regular class in its own file), than to JavaFX.

If you are getting a null on your example in line one, I’m assuming it is the “player” instance that has not been initialized yet. That would be a normal (happens all the time) cause of a NPE. Can you show where your player instance is initialized? Can you also show that you are not attempting to access any of its properties before the constructor code has finished?

Also, are we sure the variable that you initialized is the same as the one being referenced later?

A general notion (probably you already know, so more a reminder): when making changes, it is helpful to not try and do more than one or two things with each update, and test. That way one finds problems more quickly, and there is a narrower range of theories to test and investigate. For example, if you were able to go back to all inner static classes, and made just one of the classes its own file, that might be easier to troubleshoot.

After your comment, philfrei, I went back and looked more closely. Since I started out with a tutorial, I didn’t understand all of what was going on. Probably should’ve made sure I grokked it before messing too much with it.

I’m not adding the player object to the Pane, but adding the Node associated with it, which explains why I couldn’t call those methods; a Node doesn’t have them.

Still not totally clear on what’s happening here, but a Player object “player” is instantiated at the beginning of the Application class (the most outer class), then within a function that sets up the initial content in the main Pane, I go:

player = new Player();

I have no idea why this works. It’s already an instantiation of Player. Yet if I comment that out everything breaks. Clearly I have no concept of the life cycle of an object in Java.

I also found that instead of the above code I had:

Player player = new Player();

This diverges from how the tutorial did it, and seems to have caused most of my woes. This sort of crap happens when I write code after midnight after a long day. Reverting it fixed most of the issues; the program launches, enemies and asteroids spawn and behave as they should, et cetera. Still some issues, but it beats an endless stream of exceptions and a blank white window.

I know your advice was basically “make sure you’re doing what you think you are and proceed slowly”, but it made a big difference.