• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Null pointer Exception in Controller class in JavaFX  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a null pointer exception at line 16. I am trying to draw my second scene. I have a controller which switches scenes and then I assume this controller is run as soon as the scene is set.



Here is the following FXML



I have no idea what is going on.
 
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would move these lines of code:
to inside the initialize() method.  I suspect that root hasn't been injected when you get to line 16.
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...also, welcome to the Ranch!
 
Arjen Komstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I would move these lines of code:
to inside the initialize() method.  I suspect that root hasn't been injected when you get to line 16.



That worked until I tried to uncomment the three lines. This is what my code looks like now. It works up until I try to add the eventHandlers. I am really a newbie at this so please forgive me for the basicness.

 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to TellTheDetails (that's a link).  What happens?  What line?  Is there an error?  Did you post the complete error message, with the stack trace?
 
Arjen Komstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Remember to TellTheDetails (that's a link).  What happens?  What line?  Is there an error?  Did you post the complete error message, with the stack trace?



Of course! I completely forgot. It's still the same thing; A null pointer exception. Thank you for your patience!

 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the variable "scene" is null, probably because this Node (root) isn't part of the Scene.

Can we step back a bit?  Are you doing multiple Scenes with FXML loading?  How do the FXML files get loaded?  How does a specific controller know what scene to manage?
 
Arjen Komstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Looks like the variable "scene" is null, probably because this Node (root) isn't part of the Scene.

Can we step back a bit?  Are you doing multiple Scenes with FXML loading?  How do the FXML files get loaded?  How does a specific controller know what scene to manage?




Basically I have two scenes. A start screen and the game board.

Here is the code for the Main function which starts off with the start screen



I have a scene controller file which changes the scene upon clicking the button



This controller interacts with the following java file:




I only picked up Javafx two days ago so I'm doing a lot of guesswork. Do I have to load in the pacman FXML  file in the erroneous controller in the previous post?

Also, here is the fxml for the page that I am trying to add event listeners to:


 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, it took me a while to convince myself that you hadn't found a cool new way to do multi-scene JavaFX, but sadly, no.

So, I think you need a whole new approach to doing this.  To see one way that I developed, look at this GitHub repo.  You'll need to look at the pre-javafx-11 branch unless you are using JavaFX 11.

You can also search GitHub and find other ways to do this.
 
Arjen Komstad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Hey, it took me a while to convince myself that you hadn't found a cool new way to do multi-scene JavaFX, but sadly, no.

So, I think you need a whole new approach to doing this.  To see one way that I developed, look at this GitHub repo.  You'll need to look at the pre-javafx-11 branch unless you are using JavaFX 11.

You can also search GitHub and find other ways to do this.




Hahahahhahahaha

Would have been interesting if we could add this to the likes of discovering potato chips or penicillin.

Gross hyperbole aside, I really appreciate your time and effort to help me. As I will likely find out after reviewing your code, what I managed to put together today (it works somehow), needs to be reworked.

If you're interested and for the sake of completion here is the working code. I including the event handlers in the controller for the start screen.

Here it is:

 
You get good luck from rubbing the belly of a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!