扩展阶段排除故障

时间:2018-06-14 17:36:37

标签: java inheritance javafx

我创建了自己的类(StageWithData),它扩展了JavaFX Stage类,以便存储一些我将从某些传感器更新的额外信息。以下是我的Main.java

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.Random;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {


        StageWithData window = new StageWithData(primaryStage);
        FXMLLoader hpLoader = new FXMLLoader(getClass().getResource("homePage.fxml"));

        ControllerHomePage controller = hpLoader.getController();
        ***controller.setStage(window);***

        Parent root = hpLoader.load();
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
        window.setScene(scene);
        window.show();

        //This thread will be used to continuously run in the background
        //(-EDIT THIS to collect sensor data-)
        new Thread(() -> {
            while (window.isShowing()) {
                SensorDriver sDriver = new SensorDriver(window);
                sDriver.startCollection();
            }
        }).start();
    }

但由于某些原因,我在上面用***表示的行上得到了一个nullpointerexception。以下是有关我的错误的完整详细信息:

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(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
    at sample.Main.start(Main.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more
Exception running application sample.Main

这是我的StageWithDataClass:

mport javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.stage.Stage;

import java.util.Date;

public class StageWithData extends Stage {

    private SimpleStringProperty model;
    private StringProperty chassis;
    private SimpleBooleanProperty jobDone;
    private Date startTime;
    private Date endTime;

    public StageWithData(Stage window) {
        super();
        model = new SimpleStringProperty("????");
        chassis = new SimpleStringProperty("????");
        jobDone = new SimpleBooleanProperty(false);
    } ... //followed by a bunch of getters and setters

有没有办法解决这个问题?我想继续使用StageWithData,因为它可以让我设置一些全局数据。

以下是homePage.fxml的前几行 谢谢!

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

<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" stylesheets="@styles.css" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerHomePage">
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

这是我的ControllerHomePage.java

public class ControllerHomePage extends Controller {
    //Initial Declarations
    private StageWithData currWindow;

    @FXML private Button jobBut;

    @FXML private Label mcLabel;
    @FXML private Label timeLabel;

    private String model;

    //Sample.fxml functionality
    @FXML
    public void initialize() {
        System.out.println("Run Run Rudolph");
        mcLabel.textProperty().bind(currWindow.modelProperty());
    }

    public void setStage(StageWithData stage) {
        currWindow = stage;
    }

    @FXML
    private void handleJobBut(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("jobPage.fxml"));
            ControllerJobPage c = loader.getController();
            Parent root1 = loader.load();

            currWindow.setScene(new Scene(root1));
            currWindow.show();
        } catch (Exception e) {
            System.out.println("ERROR of some sort with loading jobPage.fxml");
            e.printStackTrace();

        }
    }

0 个答案:

没有答案