为JavaFX初始化时发生NullPointerException

时间:2018-11-18 14:34:16

标签: java javafx

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at MainProgram.start(MainProgram.java:25)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
    ... 1 more
Exception running application MainProgram

WorldViewer.java

public class WorldViewer {

@FXML
private ImageView worldView;

@FXML
private ImageView item;

public void Initialise() {
    Image first = new Image("1.jpg");
    Image second = new Image("2.jpg");
    worldView.setImage(first);
    item.setImage(second);
    //Image basket = new Image("basket.jpg");
    //basketView.setImage(basket);
}
}

MainProgram.java

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class MainProgram extends Application {

    public void start(Stage stage) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            String viewerFxml = "WorldViewer.fxml";
            AnchorPane page = (AnchorPane)fxmlLoader.load(this.getClass().getResource(viewerFxml).openStream());
            Scene scene = new Scene(page);
            stage.setScene(scene);

            WorldViewer viewer = (WorldViewer)fxmlLoader.getController();
            viewer.Initialise();

            stage.show();
        }
        catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
    }

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

JavaFXL文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.shape.Polygon?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button id="drop" layoutX="534.0" layoutY="360.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="51.0" text="Drop" AnchorPane.bottomAnchor="14.666666666666668" AnchorPane.rightAnchor="76.88888888888889" />
      <Button id="pickUp" layoutX="534.0" layoutY="360.0" mnemonicParsing="false" text="Pickup" AnchorPane.bottomAnchor="14.666666666666686" AnchorPane.rightAnchor="14.888888888888914" />
      <Polygon id="forward" fill="#0084ff" layoutX="297.0" layoutY="380.94772889879005" points="-10.777801513671875, -20.333328247070312, 15.444427490234375, -20.333328247070312, 2.111114501953125, -56.78310775756836" stroke="BLACK" strokeType="INSIDE" AnchorPane.bottomAnchor="39.385599348280266" />
      <Polygon id="rotateRight" fill="#0084ff" layoutX="323.0" layoutY="387.0" points="-10.777801513671875, -20.333328247070312, 13.777801513671875, -11.000030517578125, -10.777801513671875, 2.777740478515625" stroke="BLACK" strokeType="INSIDE" AnchorPane.bottomAnchor="10.222259521484375" />
      <Polygon id="rotateLeft" fill="#0084ff" layoutX="295.0" layoutY="387.0" points="-10.777801513671875, -20.333328247070312, -36.111114501953125, -9.777801513671875, -10.777801513671875, 2.777740478515625" stroke="BLACK" strokeType="INSIDE" AnchorPane.bottomAnchor="10.222259521484375" />
      <HBox id="inventory" layoutX="14.0" layoutY="334.0" prefHeight="51.0" prefWidth="150.0" AnchorPane.bottomAnchor="14.888888888888886" AnchorPane.leftAnchor="14.0" />
      <ImageView id="item" fx:id="item" fitHeight="68.0" fitWidth="65.0" layoutX="491.0" layoutY="214.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView id="worldView" fx:id="worldView" fitHeight="324.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" />
   </children>
</AnchorPane>

我有这个nullpointerexception错误。我很确定这与WorldViewer.java中的Initialize()方法有关,因为即使print语句也不会触发,但我不知道该怎么办。这些是项目文件夹中所有的文件以及我拥有的图像。
谢谢
编辑:第25行错误指向“ viewer.Initialise();”在MainProgram.java
中 我已经阅读了几篇stackoverflow文章,但似乎都没有解决我的问题

EDIT2:对其进行了修复,将fx:controller =“ WorldViewer”添加到了我的FXML中。因此,这不是链接的重复问题

0 个答案:

没有答案