i juste begin to use JavaFx for a MediaPlayer projet for school, and I need to do a VideoLibrary for movies or series + a media Player.
I am following a tutorial on treeView with Javafx and when im trying to compile my code I have this exception. "Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TreeView.setRoot(javafx.scene.control.TreeItem)" because "this.treeView" is null", and one the tutorial everything is working well for him.
Hope someone could have a look one this.
Thanks you in advance for your help.
My code is working great before I implement this code
@FXML
private TreeView treeView;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
TreeItem<String> rootItem = new TreeItem<>("Movies");
treeView.setRoot(rootItem);
}
This my Main class.
package com.example.mediaplayer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount()==2){
stage.setFullScreen(true);
}
}
});//FullScreen quand c'est cliqué deux fois
stage.setTitle("MediaPlayer");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This my controller class.
package com.example.mediaplayer;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Binding;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.chart.ValueAxis;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.scene.media.Media;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
private String pathFile;
private MediaPlayer mediaPlayer;
@FXML
private MediaView mediaView;
@FXML
private Slider progressBar;
@FXML
private Slider volumeBar;
public void choosFileMethode(ActionEvent event){
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File ('.mp4') ('.avi')","*.mp4","*.avi");
fileChooser.getExtensionFilters().add(filter);
File file = fileChooser.showOpenDialog(null);
pathFile = file.toURI().toString();
if (pathFile !=null){
Media media = new Media(pathFile);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
DoubleProperty widthProp = mediaView.fitWidthProperty();
DoubleProperty heightProp = mediaView.fitHeightProperty();
//Ajuste la taille de la fenêtre
widthProp.bind(Bindings.selectDouble(mediaView.sceneProperty(),"width"));
heightProp.bind(Bindings.selectDouble(mediaView.sceneProperty(),"height"));
mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observableValue, Duration oldValue, Duration newValue) {
progressBar.setValue(newValue.toSeconds());
}
});
progressBar.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
mediaPlayer.seek(Duration.seconds(progressBar.getValue()));
}
});
progressBar.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
mediaPlayer.seek(Duration.seconds(progressBar.getValue()));
}
});
mediaPlayer.setOnReady(new Runnable() {
@Override
public void run() {
Duration total = media.getDuration();
progressBar.setMax(total.toSeconds());
}
});
volumeBar.setValue(mediaPlayer.getVolume()*100);
volumeBar.valueProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
mediaPlayer.setVolume(volumeBar.getValue()/100);
}
});
}
}
public void playMethode(ActionEvent event){
mediaPlayer.play();
mediaPlayer.setRate(1);
}
public void pauseMethode(ActionEvent event){
mediaPlayer.pause();
}
public void stopMethode(ActionEvent event){
mediaPlayer.stop();
}
public void slowRateMethode(ActionEvent event){
mediaPlayer.setRate(0.5);
}
public void fastRateMethode(ActionEvent event){
mediaPlayer.setRate(2);
}
public void forward10second(ActionEvent event){
mediaPlayer.seek(mediaPlayer.getCurrentTime().add(Duration.seconds(10)));
}
public void downward10second(ActionEvent event){
mediaPlayer.seek(mediaPlayer.getCurrentTime().add(Duration.seconds(-10)));
}
public void exit(ActionEvent event){
System.exit(0);
}
public void ouvirListeDeLecture(ActionEvent event) throws Exception{
try{
FXMLLoader listeDeLecture = new FXMLLoader(HelloApplication.class.getResource("liste-de-lecture.fxml"));
Stage stage = new Stage();
Scene sceneListeLecture = new Scene(listeDeLecture.load(),320,240);
stage.setScene(sceneListeLecture);
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
//Liste de lecture methode
@FXML
private TreeView treeView;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
TreeItem<String> rootItem = new TreeItem<>("Movies");
treeView.setRoot(rootItem);
}
public void selectItem(ActionEvent event){
}
}
This is my Main windows with the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mediaplayer.HelloController">
<center>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />
</children>
</StackPane>
</center>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="Media">
<items>
<MenuItem mnemonicParsing="false" onAction="#choosFileMethode" text="Open a file" />
<MenuItem mnemonicParsing="false" onAction="#exit" text="Quitter" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Lecture">
<items>
<MenuItem mnemonicParsing="false" text="Avancer" />
<MenuItem mnemonicParsing="false" text="Reculer" />
<MenuItem mnemonicParsing="false" text="Lecture" />
<MenuItem mnemonicParsing="false" text="Pause" />
<MenuItem mnemonicParsing="false" text="Arreter" />
<MenuItem mnemonicParsing="false" text="Précedent" />
<MenuItem mnemonicParsing="false" text="Suivant" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Audio">
<items>
<MenuItem mnemonicParsing="false" text="Augmenter le volume" />
<MenuItem mnemonicParsing="false" text="Réduire le volume" />
<MenuItem mnemonicParsing="false" text="Couper son" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Vidéo">
<items>
<MenuItem mnemonicParsing="false" text="Plein écran" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Sous-titres">
<items>
<MenuItem mnemonicParsing="false" text="Ajouter un fichier sous-titre" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Vue">
<items>
<MenuItem mnemonicParsing="false" onAction="#ouvirListeDeLecture" text="Liste de lecture" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<bottom>
<VBox alignment="BOTTOM_CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Slider fx:id="progressBar" majorTickUnit="60.0" prefHeight="0.0" prefWidth="606.0" showTickMarks="true" />
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" onAction="#playMethode" text="Play">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#pauseMethode" text="Pause">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="51.0" layoutY="47.0" mnemonicParsing="false" onAction="#slowRateMethode" text="<<<">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="143.0" layoutY="47.0" mnemonicParsing="false" onAction="#stopMethode" text="Stop">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="102.0" layoutY="47.0" mnemonicParsing="false" onAction="#fastRateMethode" text=">>>">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="186.0" layoutY="47.0" mnemonicParsing="false" onAction="#downward10second" text="-10s">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Button layoutX="227.0" layoutY="47.0" mnemonicParsing="false" onAction="#forward10second" text="+10s">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Button>
<Slider fx:id="volumeBar">
<HBox.margin>
<Insets left="7.0" right="7.0" top="40.0" />
</HBox.margin>
</Slider>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
</VBox>
</bottom>
</BorderPane>
This my second windows when your press the button "Liste de lecture" in the menu bar menu "Vue" in the menu item "Liste de lecture".
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="532.0" prefWidth="747.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mediaplayer.HelloController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<left>
<TreeView fx:id="treeView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
And the compiler throws:
/Users/jul/Library/Java/JavaVirtualMachines/openjdk-18.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55822:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/jul/.m2/repository/org/openjfx/javafx-controls/18-ea+6/javafx-controls-18-ea+6.jar:/Users/jul/.m2/repository/org/openjfx/javafx-graphics/18-ea+6/javafx-graphics-18-ea+6.jar:/Users/jul/.m2/repository/org/openjfx/javafx-base/18-ea+6/javafx-base-18-ea+6.jar:/Users/jul/.m2/repository/org/openjfx/javafx-fxml/18-ea+6/javafx-fxml-18-ea+6.jar:/Users/jul/.m2/repository/org/openjfx/javafx-web/18-ea+6/javafx-web-18-ea+6.jar:/Users/jul/.m2/repository/org/openjfx/javafx-media/18-ea+6/javafx-media-18-ea+6.jar:/Users/jul/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0.jar:/Users/jul/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.5.32/kotlin-stdlib-common-1.5.32.jar -p /Users/jul/.m2/repository/org/kordamp/bootstrapfx/bootstrapfx-core/0.4.0/bootstrapfx-core-0.4.0.jar:/Users/jul/.m2/repository/org/controlsfx/controlsfx/11.1.1/controlsfx-11.1.1.jar:/Users/jul/.m2/repository/eu/hansolo/tilesfx/11.48/tilesfx-11.48.jar:/Users/jul/.m2/repository/org/openjfx/javafx-controls/18-ea+6/javafx-controls-18-ea+6-mac.jar:/Users/jul/.m2/repository/net/synedra/validatorfx/0.2.1/validatorfx-0.2.1.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl/17/fxgl-17.jar:/Users/jul/.m2/repository/org/kordamp/ikonli/ikonli-core/12.3.0/ikonli-core-12.3.0.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl-scene/17/fxgl-scene-17.jar:/Users/jul/Desktop/MediaPlayer/target/classes:/Users/jul/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.1/jackson-annotations-2.12.1.jar:/Users/jul/.m2/repository/org/openjfx/javafx-media/18-ea+6/javafx-media-18-ea+6-mac.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl-gameplay/17/fxgl-gameplay-17.jar:/Users/jul/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.1/jackson-core-2.12.1.jar:/Users/jul/.m2/repository/com/gluonhq/attach/audio/4.0.9/audio-4.0.9.jar:/Users/jul/.m2/repository/org/kordamp/ikonli/ikonli-javafx/12.3.0/ikonli-javafx-12.3.0.jar:/Users/jul/.m2/repository/com/gluonhq/attach/lifecycle/4.0.9/lifecycle-4.0.9.jar:/Users/jul/.m2/repository/com/dlsc/formsfx/formsfx-core/11.3.2/formsfx-core-11.3.2.jar:/Users/jul/.m2/repository/org/openjfx/javafx-base/18-ea+6/javafx-base-18-ea+6-mac.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl-entity/17/fxgl-entity-17.jar:/Users/jul/.m2/repository/com/gluonhq/attach/storage/4.0.9/storage-4.0.9.jar:/Users/jul/.m2/repository/org/openjfx/javafx-web/18-ea+6/javafx-web-18-ea+6-mac.jar:/Users/jul/.m2/repository/com/gluonhq/attach/util/4.0.9/util-4.0.9.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl-io/17/fxgl-io-17.jar:/Users/jul/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.5.32/kotlin-stdlib-1.5.32-modular.jar:/Users/jul/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.1/jackson-databind-2.12.1.jar:/Users/jul/.m2/repository/org/openjfx/javafx-fxml/18-ea+6/javafx-fxml-18-ea+6-mac.jar:/Users/jul/.m2/repository/com/github/almasb/fxgl-core/17/fxgl-core-17.jar:/Users/jul/.m2/repository/org/openjfx/javafx-graphics/18-ea+6/javafx-graphics-18-ea+6-mac.jar -m com.example.mediaplayer/com.example.mediaplayer.HelloApplication
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javafx.graphics@18-ea/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics@18-ea/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@18-ea/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics@18-ea/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: javafx.fxml.LoadException:
/Users/jul/Desktop/MediaPlayer/target/classes/com/example/mediaplayer/hello-view.fxml
at javafx.fxml@18-ea/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml@18-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml@18-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml@18-ea/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
at com.example.mediaplayer/com.example.mediaplayer.HelloApplication.start(HelloApplication.java:16)
at javafx.graphics@18-ea/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@18-ea/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@18-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@18-ea/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@18-ea/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TreeView.setRoot(javafx.scene.control.TreeItem)" because "this.treeView" is null
at com.example.mediaplayer/com.example.mediaplayer.HelloController.initialize(HelloController.java:168)
at javafx.fxml@18-ea/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
... 9 more
Exception running application com.example.mediaplayer.HelloApplication
Process finished with exit code 1