Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件读取JavaFX抛出;InvocationTargetException";?_Java_File_Exception_Javafx - Fatal编程技术网

从文件读取JavaFX抛出;InvocationTargetException";?

从文件读取JavaFX抛出;InvocationTargetException";?,java,file,exception,javafx,Java,File,Exception,Javafx,我一直在试图弄清楚到底是什么问题,但无论我做什么似乎都不管用。 我有一个文本文件,列出了名字和数字,用冒号分隔。 这方面的一个例子是: 贝蒂·罗斯:52 安琪·斯科特:29 迈克尔·罗森:72 该列表非常长,包含10000多行 public class PeopleIds { public static int UNDEFINED_ID = -1; private static HashMap<String, Integer> people; public

我一直在试图弄清楚到底是什么问题,但无论我做什么似乎都不管用。 我有一个文本文件,列出了名字和数字,用冒号分隔。 这方面的一个例子是:

贝蒂·罗斯:52

安琪·斯科特:29

迈克尔·罗森:72

该列表非常长,包含10000多行

public class PeopleIds {
    public static int UNDEFINED_ID = -1;
    private static HashMap<String, Integer> people;

    public static void initialize() {
        people = new HashMap<String, Integer>();
        System.out.println(new File("res/ids/people_ids.txt").exists());
        try {
            Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
                people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
            });
        } catch (IOException e) {
            System.out.println("Unable to read specified file.");
            e.printStackTrace();
        }
    }

    public static int getId(final String name) {
        final Integer id = people.get(name);
        return id != null ? id : UNDEFINED_ID;
    }
}
当我使用
PeopleIds.initialize()
从这个类调用它时,会抛出一个异常,告诉我应用程序启动方法中有一个异常

以下是完整记录的内容:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/Confidential/bin/base/PersonGUI.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at base.PersonGUI.start(PersonGUI.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    ... 1 more
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
    at java.io.BufferedReader$1.hasNext(Unknown Source)
    at java.util.Iterator.forEachRemaining(Unknown Source)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
    at base.PeopleIds.initialize(PeopleIds.java:17)
    at base.GUIController.initialize(GUIController.java:36)
    ... 18 more
Caused by: java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    ... 24 more
Exception running application base.PersonGUI
我不确定这是怎么回事?我已经研究过了,人们说要将fxml文件(用于格式化内容并与
GUIController
链接的文件)移动到与主类相同的包中,但它已经是了


我已经为这个问题绞尽脑汁好几天了,但都没有用。你们中有人曾经有过这个问题的经验吗?如果有,你是如何解决的?非常感谢。

如果在读取文件时,而不是在打开文件时,出现了
异常,则会对
文件.lines
流抛出一个未检查的异常操作(没有
抛出
子句)

这种情况发生在这里

Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
    people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});
,您可以在stacktrace中轻松看到:

Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
(这是由于使用了错误的
字符集
,请参阅)

您的
catch
子句没有捕获此类异常:

} catch (IOException e) {
你需要使用

} catch (Exception e) {

也捕获未检查的异常。

您是否尝试过调试?异常是否真的来自
PeopleIds.initialize();
或者更确切地说来自下面几行中的空变量?
} catch (Exception e) {