gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:29:30 26 4
gpt4 key购买 nike

我一直试图了解问题到底是什么,但无论我做什么似乎都不起作用。我有一个文本文件,其中列出了名称和数字,并用冒号分隔。一个例子是:

Betty Ross:52

Angie Scotts:29

Michael Rosen:72

该列表很长,包含 10,000 多行。

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;
}
}

我从 GUIController 类调用 initialize 方法:

public class GUIController implements Initializable {
@FXML
private TableView<PersonData> personTable;
@FXML
private TableColumn<PersonData, String> name;
@FXML
private TableColumn<PersonData, Integer> limit;
@FXML
private TextField searchInput;
@FXML
private ImageView personIcon;

private Image undefinedIcon;
private PersonIcon icon;
private ObservableList<PersonData> data;

@Override
public void initialize(URL location, ResourceBundle resources) {
PeopleIds.initialize();
undefinedIcon = new Image(getClass().getResourceAsStream("/ids/no.png"));
name.setCellValueFactory(new PropertyValueFactory<PersonData, String>("name"));
limit.setCellValueFactory(new PropertyValueFactory<PersonData, Integer>("limit"));
data = PriceData.getData();
personTable.setPeople(data);
searchInput.textProperty().addListener((ov, oldValue, newValue) -> {
final String input = searchInput.getText();
if (input.length() == 0) return;
searchInput.setText(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
filterSearch();
});
}
}

当我使用 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 链接到与 Main 类相同的包中,但是它已经是。

我已经为这个问题苦苦挣扎了好几天,但毫无结果。你们中有人有过处理这个问题的经验吗?如果是这样,你是如何解决的?非常感谢。

最佳答案

如果有Exception在读取文件时(而不是打开文件时),会为 Files.lines 抛出未经检查的异常。流操作( Stream.forEach 没有 throws 子句)。

这发生在这里

Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> {
people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", "")));
});

,您可以在堆栈跟踪中轻松看到:

Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1

(这是由于使用了错误的Charset引起的,参见Files.readAllBytes vs Files.lines getting MalformedInputException)

你不会用 catch 捕获这种异常子句:

} catch (IOException e) {

您需要使用

} catch (Exception e) {

也可以捕获未经检查的异常。

关于JavaFX 从文件读取抛出 "InvocationTargetException"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com