Объект просмотра изображения остается нулевым даже после загрузки файла f xml - PullRequest
0 голосов
/ 14 января 2020

Я делаю свой java F XML проект. Я загрузил файл View.f xml. Таким образом, объекты просмотра изображений должны быть загружены. Я пытаюсь установить всплывающую подсказку для изображения, но я получил исключение нулевого указателя. Я не знаю, почему это не работает. Кто-нибудь, помогите мне. Это мой класс контроллера.

public class Main extends Application {

    @FXML
    private ImageView imageview1;
    @FXML
    private ImageView imageview2;
    @FXML
    private ImageView imageview3;
    @FXML
    private ImageView imageview4;
    @FXML
    private Stage primarystage;
    @FXML
    private AnchorPane mainLayout;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primarystage = primaryStage;
        showView();
    }

    private void showView() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("View.fxml"));
        mainLayout = loader.load();
        Scene scene = new Scene(mainLayout);
        primarystage.setScene(scene);
        primarystage.show();
        Tooltip.install(imageview1, new Tooltip(imageview1.getAccessibleText()));
        Tooltip.install(imageview2, new Tooltip(imageview2.getAccessibleText()));
        Tooltip.install(imageview3, new Tooltip(imageview3.getAccessibleText()));
        Tooltip.install(imageview4, new Tooltip(imageview4.getAccessibleText()));

    }

    public static void main(String[] args) {
        launch(args);
    }
}

Это мой файл F xml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="Main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.Main">
   <children>
      <ImageView fx:id="imageview2" accessibleText="imageview2" fitHeight="120.0" fitWidth="200.0" layoutX="150.0" layoutY="46.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Cover.jpg" />
         </image>
      </ImageView>
      <ImageView fx:id="imageview1" accessibleText="imageview1" fitHeight="120.0" fitWidth="200.0" layoutX="13.0" layoutY="46.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@hulkvsprog.jpg" />
         </image>
      </ImageView>
      <ImageView fx:id="imageview3" accessibleText="imageview3" fitHeight="120.0" fitWidth="200.0" layoutX="300.0" layoutY="46.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@Cover.jpg" />
         </image>
      </ImageView>
      <ImageView fx:id="imageview4" accessibleText="imageview4" fitHeight="120.0" fitWidth="200.0" layoutX="442.0" layoutY="46.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../no-image.png" />
         </image>
      </ImageView>
   </children>
</AnchorPane>

Это исключение.

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at GUI.Main.showView(Main.java:52)
    at GUI.Main.start(Main.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Exception running application GUI.Main
...