这是一个使用fxml和netbeans的javafx的简单标签项目。 问题是,当我使用
时StartFomulars.setText("abc");
下面给出了一个例外情况。
以下是main的代码,
package main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Start extends Application {
@Override
public void start(Stage stage) throws Exception {
Prove Controller = new Prove();
Controller.launchController(stage);
}
public static void main(String[] args) {
launch(args);
}
}
fxml,
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1080.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<GridPane layoutX="0.0" layoutY="0.0" prefHeight="720.0" prefWidth="1080.0">
<children>
<GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<GridPane GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>
<GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="282.0" minHeight="10.0" prefHeight="282.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="281.0" minHeight="10.0" prefHeight="281.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<GridPane GridPane.columnIndex="0" GridPane.rowIndex="1">
<children>
<Label text="Given: " GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Label text="Prove: " GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label text="Goal: " GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Label fx:id="StartFomulars" text="StartFomulars" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label fx:id="GoalFomular" text="GoalFomular" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="457.0" minWidth="10.0" prefWidth="125.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="802.0" minWidth="10.0" prefWidth="790.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="188.0" minHeight="10.0" prefHeight="156.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="364.0" minHeight="10.0" prefHeight="364.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="186.0" minHeight="10.0" prefHeight="43.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="915.0" minWidth="10.0" prefWidth="915.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="539.0" minWidth="10.0" prefWidth="165.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="359.0" minHeight="10.0" prefHeight="157.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="582.0" minHeight="10.0" prefHeight="563.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
这是控制器的代码,
package main;
import ast.LogicStatement;
import java.io.IOException;
import java.util.List;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
/**
*
* @author zl2511
*/
public class Prove {
private Parent parent;
private Scene scene;
private Stage stage;
private List<LogicStatement> startStatements;
private LogicStatement goalStatement;
@FXML
private Label StartFomulars, GoalFomular;
public Prove() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Prove.fxml"));
//fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
scene = new Scene(parent);
} catch (IOException e) {
}
StartFomulars.setText("abc");
}
public void launchController(Stage stage) {
this.stage = stage;
stage.setTitle("start");
stage.setScene(scene);
stage.setResizable(true);
stage.hide();
stage.show();
}
public void redirectprove(Stage stage, List<LogicStatement> startFormulas, LogicStatement goalFormula) {
this.startStatements = startFormulas;
this.goalStatement = goalFormula;
stage.setScene(scene);
stage.hide();
stage.show();
}
}
以下是例外,
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
答案 0 :(得分:3)
你的结构很不寻常。构造Application类,FXML文件和控制器更为正常,如tutorial所示。
正如您设置的那样,您的控制器类和FXML之间没有任何关联。因此注释@FXML
的字段永远不会被初始化。
这意味着工作的方式是FXMLLoader
的{{1}}方法将解析FXML文件。如果在加载器上设置了控制器,则定义了load(...)
属性的任何元素将被注入到控制器中匹配的fx:id
带注释的字段中。
基本上有两种方法可以在@FXML
上设置控制器。第一种是在FXML文件的根元素上定义FXMLLoader
属性。这将导致FXML加载程序创建指定类的新实例,并将其用作其控制器。请注意,这不适用于您的情况,因为您希望控制器是您已创建的fx:controller
实例,而不是Prove
类的新实例。
第二种方法是创建Prove
实例(而不是依赖于您当前使用的FXMLLoader
static
方法),并在该实例上调用FXMLLoader.load(URL)
。所以(我认为)取消注释
setController(...)
行将使这个工作。
您可能还需要分隔字段的声明,即:
//fxmlLoader.setController(this);
(如果没有这种改变,它可能会有效,我从未尝试过你的方式。)
您可能还应该在@FXML
private Label StartFomulars ;
@FXML
private Label GoalFomular;
的构造函数中取出try {...} catch (...) {...}
结构(因为无论如何都要声明它Prove
),或者至少在throws IOException
中执行某些操作块。现在,如果加载FXML会发生异常,你就不会知道它了,你只需继续前进并尝试设置标签的文本。