我启动Java FX,我不明白为什么我的gridpane为null
并导致我java.lang.NullPointerException
(在grido.add(tf1,2,2);
行)
我做了很多改变而没有任何结果。
感谢您的帮助。
keepSpaceFXML.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane id="fx:grido" xmlns:fx="http://javafx.com/fxml/1">
<Button GridPane.columnIndex="0" GridPane.rowIndex="0" text="coucou2" onAction="#handleButtonAction" fx:id="button" />
</GridPane>
KeepSpaceFXML.java
package keepspacefxml;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
public class KeepSpaceFXML extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("keepSpaceFXML.fxml"));
KeepSpaceGridController gridController = new KeepSpaceGridController();
loader.setController(gridController);
Parent root = loader.load();
System.err.println("gridController");
System.err.println(gridController);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
gridController.addRow("hello gridpane !");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
KeepSpaceGridController.java
package keepspacefxml;
import java.net.URL;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class KeepSpaceGridController {
@FXML public GridPane grido;
@FXML private Label label;
public void addRow(String text) {
System.out.println("add row");
TextField tf1 = new TextField("tf 1");
System.err.println("grido");
System.err.println(grido);
grido.add(tf1,2,2);
}
public void setLabel(String text) {
label.setText(text);
}
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("clicked !");
}
}
输出是:
ant -f /home/doom/Documents/javafxtests/keepSpaceFXML jfxsa-run
init:
Deleting: /home/doom/Documents/javafxtests/keepSpaceFXML/build/built-jar.properties
deps-jar:
Updating property file: /home/doom/Documents/javafxtests/keepSpaceFXML/build/built-jar.properties
Compiling 1 source file to /home/doom/Documents/javafxtests/keepSpaceFXML/build/classes
compile:
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from /home/doom/jdk1.8.0_141/jre/../lib/ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from /home/doom/jdk1.8.0_141/jre/../lib/ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 12 files to /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249
jfx-project-run:
Executing /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249/keepSpaceFXML.jar using platform /home/doom/jdk1.8.0_141/jre/bin/java
gridController
keepspacefxml.KeepSpaceGridController@35d79ed8
add row
grido
null
Exception in Application start method
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:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at keepspacefxml.KeepSpaceGridController.addRow(KeepSpaceGridController.java:51)
at keepspacefxml.KeepSpaceFXML.start(KeepSpaceFXML.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
... 1 more
Exception running application keepspacefxml.KeepSpaceFXML
Java Result: 1
Deleting directory /home/doom/Documents/javafxtests/keepSpaceFXML/dist/run2075630249
jfxsa-run:
BUILD SUCCESSFUL (total time: 1 second)
答案 0 :(得分:-1)
我不知道你想要做什么,但首先你的控制器需要实现 Initializable 接口,并实现初始化方法。
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class KeepSpaceGridController implements Initializable {
@FXML
public GridPane grido;
@FXML
private Label label;
@Override
public void initialize(URL location, ResourceBundle resources) {
addRow("add row");
}
public void addRow(String text) {
System.out.println("add row");
TextField tf1 = new TextField("tf 1");
grido.add(tf1, 2, 2);
}
public void setLabel(String text) {
label.setText(text);
}
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("clicked !");
}
}