Stephan van Hulst wrote:It looks like you're completely confused about the lifecycle of an FXML controller.
A controller is created by JavaFX when the FXMLLoader loads the FXML of the user interface that is controlled by the controller. It goes like this:
User calls FXMLLoader.load() for a given user interface FXML definition. The loader reads the FXML and determines the class of the controller that will control the user interface. The loader creates an instance of the controller class and calls its constructor. The loader creates instances of all the JavaFX components needed in the user interface. Components of the user interface are injected into fields of the controller instance that are annotated with @FXML. After all components are done being created and injected into the controller, the loader calls the controller's initialize() method. Execution returns to the method that called FXMLLoader.load(). This method now can access both the user interface and the controller associated with it.
With this in mind, here's why your code is not working:
In VideoSelectionController, when you click on an image, you use FXMLLoader to create a new user interface, but you completely ignore its associated controller instance and instead create your own manually. You must never create your own controller instances, because they won't be associated with a user interface. Your calls to videoController.setMedia() and videoController.videoPlay() won't do anything because the controller doesn't actually control anything.
Another thing: Why are you loading another instance of the VideoSelectionController in your VideoPlayerController and then ignore it? I don't see what business the video player has with the video selector when it's supposed to play a video, so you can probably remove all of that code.
Your videolLoop() code is a bit messy and it is poorly named. It looks like you're trying to match the file names of a video and an image from two different arrays. Why? Do you ever have a case where one array contains different file names than another (ignoring file name extension)? I would just use a single collection that contains the file names without extension and then construct the paths of both the image and the video from that. Also, if the resources (not ressources) are packaged with your application and should not be overwritable after the application has been installed on a client system, you really should be using resource paths and not file paths. It looks like you're not using Maven to build your application. Why not?
If you use Maven, your source code files would be under src/main/java and you could put your FXML under src/main/resources/view and your images under src/main/resources/images, etc. The code to initialize your thumbnails would then look something like this:
Stephan van Hulst wrote:Your problem is caused because you're trying to call new MediaPlayer(media) in the initialize() method of your videoSelectionController, but media hasn't been assigned a value yet.
Don't create a MediaPlayer in the initialize() method. Create one inside the videoPlay() method.
Also, please follow proper Java naming conventions. VideoSelectionController should be written with a capital letter.
Paul Clapham wrote:
jack turret wrote:
First of all you have a relative path here. So it's possible that its answer to the question "What's this path relative to?" is different than your answer, and so the media isn't found. That would cause the NPE. Likewise there's some obscure code which produces a file name, which also raises the possibility of not finding the media and resulting in an NPE.
All of this business of restructuring the design and code may or may not be relevant; it may just be that you have a bad path in your code.
jack turret wrote:I debugged my code and noticed that videoplayer is null but I can not think of a reason why.
Stephan van Hulst wrote:That is because you're not using the resource path correctly. The resource path is not a file system path. It is a path inside a Java package of classes.
Assuming that the src folder is the root of your package hierarchy, the path to use is "/view/videoPlayerView.fxml". Don't forget the forward slash at the start, otherwise Java will look for your resource relative to the package that your controller class is in.
Stephan van Hulst wrote:Ahh sorry. Rename getWindow(Event event) to getStage(Event event) and change its implementation to this:
The rest of your code can remain the same.
Stephan van Hulst wrote:Yeah, but the initialize() method is probably never called. That's because you are creating the controller class manually, instead of letting FXMLLoader do it.
Don't create controllers by calling their constructors. Ditch viewSwitcherMethod() and instead create the new scene as I've shown you.
Stephan van Hulst wrote:What does your view switcher do? I feel that your problem is caused because you are not using the regular way to instantiate FXML controllers. Also, you need to treat the initialize() method like you treat constructors: Use them for initialization of fields ONLY. Don't perform any business logic in constructors or initializers.
You'll want to do something like this:
EventUtilities
Campbell Ritchie wrote:JT: please read this.