• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to get an instance of a controller class and use one of its methods

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I found a lot of answered questions about this topic but nothing seems to work for me and I just cant get my head around it. The idea is to clear a StackPane (changablePane in my code) so that another item can be displayed depending on the selected item in the filter combo boxes.

Here is my code

MainWindowController.java



MainWindow.fxml



NotesScreenController.java



NotesScreen.fxml



Everything i tried gives an nullPointerException.
 
Greenhorn
Posts: 11
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Filip,

I commend you for working with JavaFX- it's pretty nifty, and offers a lot of advantageous over AWT/swing. I do have to say though mate, you posted a lot of code. JavaFX with the FXML allows for dynamic run-time loading, so you don't have to use as much code-behind. This is convenient, but there's several places where a variable could be uninitialized. Have you tried running this in debug? Where does the program get to right before it gets a NullPointer? A little more detail of the line the exception is occurring on, or the calling function would be very helpful here.

Thanks,

-Grant
 
Filip Kekev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Grant. I should have mentioned where i get the nullPointerException and post the code that i used to try and get an instance of the MainWindowController, sorry for that. Wtih the code above i got a nullPointer in the call of mainController.clearPane. I tried a different approach:



and now i get a null pointer exception in the clearPane method in mainWindowController.
Here is the whole stacktrace of the nullPointer



My plan is to use the same code for a bunch of other things in my app. If you think that i should try a different way and somehow restructure my app please let me know, i would really appreciate any suggestion.

Thanks
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect this is the line causing the problem.  What happens when nothing is selected in the sort box?
 
Filip Kekev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the user opens the window the sort box is empty, and then he selects something which should run the code from the mainWindowController. I know that the problem is with the way I'm getting the instance of the controller so I made some changes and now the changablePane is not created by the fxml file, I'm creating it by java code and made it static. That way I can make the clearPane() mehod static and call it from another class without creating an instance of the MainWindowCotroller. Now the problem is that the app doesnt show the changablePane with the filtered notes after it clears it.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think code like this is safer:
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Filip Kekev wrote:When the user opens the window the sort box is empty, and then he selects something which should run the code from the mainWindowController. I know that the problem is with the way I'm getting the instance of the controller so I made some changes and now the changablePane is not created by the fxml file, I'm creating it by java code and made it static. That way I can make the clearPane() mehod static and call it from another class without creating an instance of the MainWindowCotroller. Now the problem is that the app doesnt show the changablePane with the filtered notes after it clears it.


Can you post your code?
 
Filip Kekev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Knute, I managed to get the functionality that i wanted with using static getter methods that i call from NotesScreenController. Here is the code:

MainScreenController:



NotesScreenController

 
My pie came with a little toothpic holding up this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic