• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Filip Kekev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Grant Callant
Greenhorn
Posts: 11
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think code like this is safer:
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!