即使加载了fxml文件

我正在做我的Java FXML项目。我加载了View.fxml文件。因此,应加载imageview对象。我正在尝试为imageview安装工具提示,但出现了空指针异常。我不知道为什么它不起作用。有人帮我。 这是我的Controller类。

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);
    }
}

这是我的Fxml文件

<?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
lmzxd 回答:即使加载了fxml文件

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2768956.html

大家都在问