你好,我是Java语言的新手。我不断收到此错误
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.io.FileNotFoundException: Test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at cw3.Controller.loadPressed(Controller.java:48)
所以我想做的是使Java读取文本文件,然后使用内部信息计算均值。 这是我的代码main.java
package cw3;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Calc.fxml"));
primaryStage.setTitle("TEST");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
Controller.java
package cw3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
//To open the load window
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Controller {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="load"
private Button load; // Value injected by FXMLLoader
@FXML // fx:id="mean"
private TextArea mean; // Value injected by FXMLLoader
@FXML
void loadPressed(ActionEvent event) throws FileNotFoundException, IOException{
FileChooser myfileChooser = new FileChooser();
myfileChooser.setTitle("Open...");
File FileDestination = myfileChooser.showOpenDialog(new Stage());
String filename = FileDestination.getName();
System.out.print(filename);
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(filename);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputFileLine = null;
double Mean = 0;
int totalnumber = 0;
while ((inputFileLine = bufferedReader.readLine()) != null) {
Mean += Double.valueOf(inputFileLine);
totalnumber++;
}
double showmean = Mean/totalnumber;
mean.setText(String.valueOf(showmean));
}
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert load != null : "fx:id=\"load\" was not injected: check your FXML file 'Calc.fxml'.";
assert mean != null : "fx:id=\"mean\" was not injected: check your FXML file 'Calc.fxml'.";
}
}
请帮助我解决此错误,这有点令人困惑。在加载窗口中点击打开按钮后,我收到此错误。
P.S。我使用scenebuilder应用程序和netbean来开发Java应用程序。如果您想让我知道fxml文件,我相信fxml文件并不重要。
答案 0 :(得分:2)
根本错误是:
Caused by: java.io.FileNotFoundException: Test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at cw3.Controller.loadPressed(Controller.java:48)
这是说文件Test.txt
不存在,并且错误正在您的48
类的行cw3.Controller
上抛出。请查看What is a stack trace, and how can I use it to debug my application errors?问答以获取有关堆栈跟踪以及如何读取它们的更多信息。
在您的代码中:
@FXML
void loadPressed(ActionEvent event) throws FileNotFoundException, IOException{
FileChooser myfileChooser = new FileChooser();
myfileChooser.setTitle("Open...");
File FileDestination = myfileChooser.showOpenDialog(new Stage());
String filename = FileDestination.getName();
System.out.print(filename);
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(filename);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputFileLine = null;
double Mean = 0;
int totalnumber = 0;
while ((inputFileLine = bufferedReader.readLine()) != null) {
Mean += Double.valueOf(inputFileLine);
totalnumber++;
}
double showmean = Mean/totalnumber;
mean.setText(String.valueOf(showmean));
}
您正在将FileDestination.getName()
的结果(顺便说一下,根据Java的命名约定,变量名以小写字母开头)传递给FileReader
构造函数。 File#getName()
的文档内容如下:
返回此抽象路径名表示的文件或目录的名称。 这只是路径名名称序列中的姓氏(添加了重点)。如果路径名的名称序列为空,则返回空字符串。
您需要通过完整路径。您可以通过File#getPath()
执行此操作,但仅使用FileReader#<init>(File)
构造函数并直接传递File
对象可能会更容易。您还需要记住在完成操作后关闭FileReader
;最简单的方法是使用try-with-resources statement。例如:
File chosenFile = ...;
try (BufferedReader br = new BufferedReader(new FileReader(chosenFile)) {
// read from the BufferedReader
}
// Note: Closing the BufferedReader will close the nested FileReader
其他一些注意事项:
您可以使用NIO.2 API通过Path
将BufferedReader
转换为File#toPath()
,然后使用{{3}来打开File
}。
Files#newBufferedReader(Path)
(完成操作后不要忘记关闭Stream
)。您的方法正在 JavaFX Application线程(即UI线程)上执行I / O工作。您永远都不想在FX线程上进行阻塞或长时间运行,因为这会冻结UI,这不是良好的用户体验。考虑将I / O工作移至后台线程,然后将结果发布回FX线程。有关更多信息,请参见Files#lines(Path)
和Concurrency in JavaFX软件包。
您需要处理FileChooser#showOpenDialog(Window)
返回null
的情况。如果用户从对话框中取消,则会发生这种情况。目前,您的代码只会抛出NullPointerException
。
请勿将刚刚实例化的随机Stage
传递给#showOpenDialog(Window)
。您应该传递适当的窗口实例(通常是按钮/任何按钮所属的窗口),以使模式正确运行(即,在显示文件选择器对话框时,防止用户与所有者窗口进行交互)。每个Node
都有对其Scene
的引用,而该引用也有对其Window
的引用。您可以使用load.getScene().getWindow()
。