1

I've been trying to understand what the issue is exactly, but whatever I do doesn't seem to work. I have a text file that lists names along with numbers, seperated by a colon. An example of this is:

Betty Ross:52

Angie Scotts:29

Michael Rosen:72

The list is very long and comprises over 10,000 lines.

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

I call the initialize method from a GUIController class:

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

When I call it from this class with PeopleIds.initialize(), an exception is thrown, telling me that there was an exception in the application start method.

Here is what was logged in its entirety:

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

I'm not sure what is going on here? I've looked into it and people have said to move the fxml file (the one that is used to format the content and is linked with the GUIController to the same package as the Main class, however it already is.

I've been wrestling with this issue for days to no avail. Do any of you have past experiences with this issue? If so, how did you resolve it? Thanks a lot.

1
  • have you tried to debug? does the exception really come from PeopleIds.initialize(); or maybe rather from your variables being null in the lines below that? Commented Apr 1, 2016 at 9:41

1 Answer 1

1

If there is an Exception while the file is being read, not when opening the file, an unchecked exception is thrown for the Files.lines stream operation (Stream.forEach doesn't have a throws clause).

This happens here

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

, which you can easily see in the stacktrace:

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

(This is caused by the wrong Charset being used, see Files.readAllBytes vs Files.lines getting MalformedInputException )

You don't catch this kind of exception with your catch clause:

} catch (IOException e) {

You need to use

} catch (Exception e) {

to catch the unchecked exceptions too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.