英文:
JavaFX: Exception in thread “JavaFX Application Thread” java.lang.RuntimeException: java.lang.reflect.InvocationTargetException Ask Question
问题
我正在尝试创建一个简单的程序,以便在两个字段中接收用户输入,然后将其保存为字符串并写入文件。
我遇到了一个我不认识的错误,而且无论我怎么找问题,都找不到。希望有人能够解释一下。
我的Controller.java文件:
package sample;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Controller {
@FXML
private TextField InputEmail;
@FXML
private TextField InputName;
// 输出写入的字符串
public String Output;
public void handleButtonAction(ActionEvent event) {
Output = "Student Name: " + InputName.getText() + "|| Student Email: " + InputEmail.getText() + "\n";
}
public void Add(ActionEvent event) {
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.write(Output);
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
}
我的sample.fxml文件:
<!-- 略 -->
我的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.io.IOException;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) throws IOException {
launch(args);
}
}
当我尝试使用 "Add" 按钮并写入表单时,会抛出以下错误:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.NullPointerException
...
如果有人能提供帮助,我将非常感激,因为我是新手。
英文:
Im trying to create a simple program to take user input in 2 fields, then save it as a string and write it to a file.
Im getting an error that I dont recognize and cant find the problem anywhere, hopefully someone can explain it to me.
My Controller.Java
import java.io.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Controller {
@FXML
private TextField InputEmail;
@FXML
private TextField InputName;
//Output writer String
public String Output;
public void handleButtonAction(ActionEvent event) {
Output = "Student Name: " + InputName.getText() + "|| Student Email: " + InputEmail.getText() + "\n";
}
public void Add(ActionEvent event) {
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.write(Output);
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
}
My sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<SplitPane dividerPositions="0.25252525252525254, 0.5252525252525253" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="12.0" layoutY="12.0" text="Create Student" textFill="#32d9ff">
<font>
<Font name="Arial" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextField id="InputName" fx:id="InputName" layoutX="5.0" layoutY="13.0" onInputMethodTextChanged="#handleButtonAction" promptText="Student Firstname" />
</children>
</AnchorPane>
<AnchorPane layoutX="10.0" layoutY="10.0" minHeight="0.0" minWidth="0.0" prefHeight="68.0" prefWidth="158.0">
<children>
<TextField id="InputEmail" fx:id="InputEmail" layoutX="5.0" layoutY="14.0" onAction="#handleButtonAction" promptText="Student Email" />
<Button id = "button" fx:id="button" layoutX="60.0" layoutY="46.0" mnemonicParsing="false" text="Add" onAction="#Add" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
</GridPane>
My Main.Java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) throws IOException {
launch(args);
}
}
And finally the error thats thrown when I try to use the "Add" button and write the form.
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
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:835)
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:567)
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:567)
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$MethodHandler.invoke(FXMLLoader.java:1782)
... 51 more
Caused by: java.lang.NullPointerException
at java.base/java.io.Writer.write(Writer.java:249)
at sample.Controller.Add(Controller.java:29)
... 62 more
Any help is greatly appreciated as I am new to Java.
答案1
得分: 1
根据情况,您的程序似乎从未设置Output的值(可以在方法顶部使用System.out.println(Output)
进行确认)。也许您可以考虑将handleButtonAction
方法中的所有逻辑放入Add
方法中,而不必使用一个字符串来表示Output,具体做法如下:
public void Add(ActionEvent event) {
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.append("Student Name: " + InputName.getText() + "|| Student Email: " + InputEmail.getText() + "\n");
fw.close();
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
或者,您可以将输出字符串的赋值移到add()
方法中,具体做法如下:
public void Add(ActionEvent event) {
output = "Student Name: " + InputName.getText() + "|| Student Email: " +
InputEmail.getText() + "\n";
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.append(output);
fw.close();
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
append(String s)
方法是追加而不是覆盖内容的,使用哪个取决于您的需求。close()
方法将流刷新到文件(保存内容)并关闭流。
英文:
By the looks of it your program is never setting the value of Output (confirm with System.out.println(Output)
at the top of the method), maybe instead of having a string for Output at all you just put all the logic from the handleButtonAction method in Add like so
public void Add(ActionEvent event) {
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.append("Student Name: " + InputName.getText() + "|| Student Email: " + InputEmail.getText() + "\n");
fw.close();
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
Or move the output string assignment to add()
public void Add(ActionEvent event) {
output ="Student Name: " + InputName.getText() + "|| Student Email: " +
InputEmail.getText() + "\n"
try {
FileWriter fw = new FileWriter("D:/Assignment3&4_Output.txt");
fw.append(output);
fw.close();
System.out.println("File updated");
} catch (IOException e) {
System.out.println("File could not be found. Please check the file path.");
}
}
The append(String s)
method adds rather than overwrites, which you use is optional
the close()
method flushes the stream into the file (saves it) and closes the stream.
P.S. <a href="https://www.javatpoint.com/java-naming-conventions">You should make the first letter of everything except classes lowercase</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论