简体   繁体   中英

Java: Program not able to list contents of txt file when reading from it

I'm working on a program that uses JavaFx to display icons in a list. I've made a static class used to look up specific ids from a txt document. Originally, the static block would add the id and name of an item defined on each line, but since these issues arose, I've tried to find the source of the issue.

Instead, I've just gone through the text file's content in the static block and have printed it out to the console.

This is my code for reference:

public class ItemIds {
    public static int UNDEFINED_ID = -1;
    private static HashMap<String, Integer> items;
    static {
        items = new HashMap<String, Integer>();
        System.out.println(new File("res/ids/item ids.txt").exists());
        try {
            //should print out every line in the text file
            Files.lines(Paths.get("res/ids/item ids.txt")).forEach(s -> {
                System.out.println(s);
            });
        } catch (IOException e) {
            System.out.println("Unable to read specified file.");
            e.printStackTrace();
        }
    }

    public static int getId(final String name) {
        final Integer id = items.get(name);
        return id != null ? id : UNDEFINED_ID;
    }
}

However, what I do get when this static class is initialized and the static block is invoked is quite odd. It lists every single line without error until it gets to line 10691, where it throws "Exception in thread "JavaFX Application Thread" java.lang.ExceptionInInitializerError".

What makes this particularly weird, however, is that when I work with a smaller text document (with less entries), everything seems to work fine. Since the file is comprised of almost 14000 lines, I have to delete ~4000 lines for it to be able to work.

Any ideas on why it would be doing this? Any feedback is appreciated - thank you

I am unable to reproduce this error. I have created a file with 18K lines and you program just works fine with that. So, definitely consider reviewing your file and also the stack trace.

Now coming back to your exception ExceptionInInitializerError , the following is a possible:

ExceptionInInitializerError signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

class ItemIds
{
  static
  {
     // if something does wrong -> ExceptionInInitializerError
  }
}

Because static variables are initialized in static blocks there is a potential for introducing errors too. An example:

class ItemIds
{
  static int v = D.foo();
}

=>

class ItemIds
{
  static int v;

  static
  {
    v = D.foo();
  }
}

So if foo() goes crazy then you can get a ExceptionInInitializerError.

Have you presented your complete code in static block?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM