[由@kai解决],解决方案:在PersonOverview.fxml上将ID切换为fx:id
我是新来的。
我正在尝试运行此example
在教程的第一部分,一切都很好。我已经运行调试并检查填充TableView的变量,并且所有变量都不为空。
我检查了此链接,并解决了一些以前的错误,将此导出添加到module-info.java。我看到了这个link来做到这一点。我已经阅读了有关使用jdk 11的非模块应用程序的信息,这些模块需要module-info.java显式导出javafx模块并将我的包注入这些模块中。效果很好。
但是现在我迷路了。
我正在使用:
IDE: Eclipse 2018-12 with plugin e(fx)clipse
java -version:
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environmente 18.9 (build 11.0.2+9)
OpenJDK 64-bit Server VM 18.9 (build 11.0.2+9, mixed mode)
javac -version:
javac 11.0.2
JavaFX Scene Builder 2.0
档案:
================ module-info.java ================
module ninja.plata.agenda{
requires javafx.controls;
requires transitive javafx.graphics;
requires javafx.fxml;
requires transitive javafx.base;
requires java.base;
opens ninja.plata.agenda.view to javafx.fxml;
exports ninja.plata.agenda to javafx.controls, javafx.graphics, javafx.fxml;
exports ninja.plata.agenda.view to javafx.fxml;
exports ninja.plata.agenda.controller to javafx.fxml;
exports ninja.plata.agenda.model to javafx.fxml;
}
================ ninja.plata.agenda.MainApp.java ================
package ninja.plata.agenda;
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import ninja.plata.agenda.model.Person;
import ninja.plata.agenda.view.PersonOverviewController;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
/**
* Os dados como uma observable list de Persons.
*/
@FXML
private ObservableList<Person> personData = FXCollections.observableArrayList();
/**
* Construtor
*/
public MainApp() {
// Add some sample data
personData.add(new Person("Hans", "Muster"));
personData.add(new Person("Ruth", "Mueller"));
personData.add(new Person("Heinz", "Kurz"));
personData.add(new Person("Cornelia", "Meier"));
personData.add(new Person("Werner", "Meyer"));
personData.add(new Person("Lydia", "Kunz"));
personData.add(new Person("Anna", "Best"));
personData.add(new Person("Stefan", "Meier"));
personData.add(new Person("Martin", "Mueller"));
}
/**
* Retorna os dados como uma observable list de Persons.
* @return
*/
public ObservableList<Person> getPersonData() {
return personData;
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Person Book");
initRootLayout();
showPersonOverview();
}
/**
* Inicializa o root layout (layout base).
*/
public void initRootLayout() {
try {
// Carrega o root layout do arquivo fxml.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/App.fxml"));
rootLayout = (BorderPane) loader.load();
// Mostra a scene (cena) contendo o root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
//scene.setRoot(rootLayout);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Mostra o person overview dentro do root layout.
*/
public void showPersonOverview() {
try {
// Carrega a person overview
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Define a person overview no centro do root layout.
rootLayout.setCenter(personOverview);
// Dá ao controlador acesso à the main app.
PersonOverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Retorna o palco principal.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
================ ninja.plata.agenda.view.PersonOverviewController ================
package ninja.plata.agenda.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import ninja.plata.agenda.MainApp;
import ninja.plata.agenda.model.Person;
public class PersonOverviewController {
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
@FXML
private Label firstNameLabel;
@FXML
private Label lastNameLabel;
@FXML
private Label streetLabel;
@FXML
private Label postalCodeLabel;
@FXML
private Label cityLabel;
@FXML
private Label birthdayLabel;
// Reference to the main application.
private MainApp mainApp;
/**
* O construtor.
* O construtor é chamado antes do método inicialize().
*/
public PersonOverviewController() {
}
/**
* Inicializa a classe controller. Este método é chamado automaticamente
* após o arquivo fxml ter sido carregado.
*/
@FXML
public void initialize() {
// Inicializa a tabela de pessoa com duas colunas.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
}
/**
* É chamado pela aplicação principal para dar uma referência de volta a si mesmo.
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Adiciona os dados da observable list na tabela
personTable.setItems(mainApp.getPersonData());
}
}
================ PersonOverview.fxml ================
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ninja.plata.agenda.view.PersonOverviewController">
<children>
<SplitPane dividerPositions="0.29797979797979796" layoutX="128.0" layoutY="60.0" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView id="personTable" layoutX="-25.0" layoutY="-31.0" prefHeight="298.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn id="firstNameColumn" prefWidth="87.0" text="First name" />
<TableColumn id="lastNameColumn" prefWidth="87.0" text="Last name" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Person details:" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" />
<GridPane layoutX="42.0" layoutY="77.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First name" />
<Label text="Last name" GridPane.rowIndex="1" />
<Label text="Street" GridPane.rowIndex="2" />
<Label text="City" GridPane.rowIndex="3" />
<Label text="Postal code" GridPane.rowIndex="4" />
<Label text="Birthday" GridPane.rowIndex="5" />
<Label id="firstNameLabel" text="firstNameLabel" GridPane.columnIndex="1" />
<Label id="lastNameLabel" text="lastNameLabel" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label id="streetLabel" text="streetLabel" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label id="cityLabel" text="cityLabel" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label id="postalCodeLabel" text="postalCodeLabel" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label id="birthdayLabel" text="birthdayLabel" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
<HBox layoutX="195.0" layoutY="248.0" spacing="10.0" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<Button layoutX="195.0" layoutY="248.0" mnemonicParsing="false" text="New" />
<Button layoutX="269.0" layoutY="248.0" mnemonicParsing="false" text="Edit" />
<Button layoutX="340.0" layoutY="248.0" mnemonicParsing="false" text="Delete" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
错误:
javafx.fxml.LoadException:
/C:/Users/Gabriel/eclipse-workspace-open-jdk11/agenda/bin/ninja/plata/agenda/view/PersonOverview.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at ninja.plata.agenda/ninja.plata.agenda.MainApp.showPersonOverview(MainApp.java:88)
at ninja.plata.agenda/ninja.plata.agenda.MainApp.start(MainApp.java:57)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: 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:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
... 13 more
Caused by: java.lang.NullPointerException
at ninja.plata.agenda/ninja.plata.agenda.view.PersonOverviewController.initialize(PersonOverviewController.java:48)
... 24 more