Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JavaFX: How do I access the methods of an object owned by a pane?  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Offline Alletarchus

Junior Newbie

Medals: 1

« Posted 2018-02-20 02:15:34 »

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

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:


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.

Offline philfrei
« Reply #1 - Posted 2018-02-20 05:03:14 »

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.

music and music apps:
Offline Alletarchus

Junior Newbie

Medals: 1

« Reply #2 - Posted 2018-02-20 06:55:08 »

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.
Pages: [1]
  ignore  |  Print  

hadezbladez (288 views)
2018-11-16 13:46:03

hadezbladez (157 views)
2018-11-16 13:41:33

hadezbladez (289 views)
2018-11-16 13:35:35

hadezbladez (70 views)
2018-11-16 13:32:03

EgonOlsen (2133 views)
2018-06-10 19:43:48

EgonOlsen (2152 views)
2018-06-10 19:43:44

EgonOlsen (1364 views)
2018-06-10 19:43:20

DesertCoockie (1966 views)
2018-05-13 18:23:11

nelsongames (1599 views)
2018-04-24 18:15:36

nelsongames (2247 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!