我的项目非常简单。它有两个名为latField
和longField
的TextField。我要做的就是使用getText()
方法打印这些字段中包含的字符串值,并偶尔使用setText()
方法设置它们。
请注意,我正在使用javafx场景构建器,并且有一个FXMLDocument
和FXMLDocumentController
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: #eee;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pokemongocontroller.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="114.0" layoutY="92.0" minHeight="16" minWidth="69" />
<Button id="upButton" layoutX="140.0" layoutY="84.0" mnemonicParsing="false" onAction="#upButtonClick" prefHeight="36.0" prefWidth="38.0" text="U" />
<Button id="downButton" layoutX="140.0" layoutY="140.0" mnemonicParsing="false" onAction="#downButtonClick" prefHeight="36.0" prefWidth="38.0" text="D" />
<Button id="leftButton" layoutX="94.0" layoutY="106.0" mnemonicParsing="false" onAction="#leftButtonClick" prefHeight="36.0" prefWidth="38.0" text="L" />
<Button id="rightButton" layoutX="188.0" layoutY="106.0" mnemonicParsing="false" onAction="#rightButtonClick" prefHeight="36.0" prefWidth="38.0" text="R" />
<TextField id="latField" layoutX="25.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />
<TextField id="longField" layoutX="98.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />
<Label layoutX="47.0" layoutY="49.0" text="Lat" />
<Label layoutX="119.0" layoutY="49.0" text="Long" />
<Button id="goButton" layoutX="188.0" layoutY="28.0" mnemonicParsing="false" onAction="#goButtonClick" prefHeight="28.0" prefWidth="78.0" text="GO!" />
<TextField id="text" layoutX="47.0" layoutY="158.0" prefHeight="22.0" prefWidth="69.0" text="dfgdfg" />
</children>
</AnchorPane>
FXMLDocumentController.java
public class FXMLDocumentController implements Initializable {
private double delta=0.00001;
private double latitude,longitude;
@FXML
private Button upButton,downButton,rightButton,leftButton,goButton;
private TextField latField;
private TextField longField;
@FXML
private void upButtonClick(ActionEvent event) {
System.out.println("You clicked U!");
latitude+=delta;
printCoordinates();
}
@FXML
private void downButtonClick(ActionEvent event) {
System.out.println("You clicked D!");
latitude-=delta;
printCoordinates();
}
@FXML
private void leftButtonClick(ActionEvent event) {
System.out.println("You clicked L!");
longitude-=delta;
printCoordinates();
}
@FXML
private void rightButtonClick(ActionEvent event) {
System.out.println("You clicked R!");
longitude+=delta;
printCoordinates();
}
@FXML
private void goButtonClick(ActionEvent event) {
System.out.println("You clicked Go!");
latitude= Double.parseDouble(latField.getText());
longitude= Double.parseDouble(longField.getText());
printCoordinates();
System.out.println(text.getText());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
latField.setText("0.0");
longField.setText("0.0");
latitude= Double.parseDouble(latField.getText());
longitude= Double.parseDouble(longField.getText());
printCoordinates();
System.out.println(text.getText());
}
private void printCoordinates(){
System.out.println("Latitude: "+latitude);
System.out.println("Longitude: "+longitude);
}
}
我在以下行中得到Null Pointer Exception -
latField.setText("0.0");
完整Stacktrace -
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:497)
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:497)
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$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1343441044.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/E:/PokemonGo/PokemonGoController/dist/run583471250/PokemonGoController.jar!/pokemongocontroller/FXMLDocument.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at pokemongocontroller.PokemonGoController.start(PokemonGoController.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/107178178.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1823905343.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
... 1 more
Caused by: java.lang.NullPointerException
at pokemongocontroller.FXMLDocumentController.initialize(FXMLDocumentController.java:84)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 22 more
Exception running application myapplication.ApplicationController
Java Result: 1
这是我的ApplcationController.java
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
/**
*
* @author Lomesh
*/
public class ApplicationController extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我的理解是FXML中定义的控件没有注入FXMLDocumentController.java但不知道原因。的 WHY吗
答案 0 :(得分:6)
(代表OP发布)。
现在已经解决了。问题在于fxml文档中的var treeWalker = document.createNodeIterator(
document.querySelector(".context"),
NodeFilter.SHOW_TEXT,
function(){
return NodeFilter.FILTER_ACCEPT;
},
false
);
属性。
id
如果使用场景构建器指定,则默认情况下会生成<TextField id="latField" layoutX="25.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />
属性。我注意到在另一个工作项目中,它是id
。不知道为什么场景构建器不一致。
摘要:使用fx:id
代替fx:id
。
答案 1 :(得分:1)
@FXML private Button upButton,downButton,rightButton,leftButton,goButton;
对我来说这种方式不起作用。你需要这样做:
@FXML private Button upButton;
...
@FXML private Button goButton;
答案 2 :(得分:0)
这是注入问题。确保控件的id和@FXML控件变量名相同。
答案 3 :(得分:0)
另一个需要注意的问题是fxml的id名称必须与Controller的id名称相等。