在开关内构造对象时创建 java.lang.ExceptionInInitializerError

标签 java switch-statement runtime-error

在开关内,我创建了 Hive 类的一个实例,在创建对象后,它返回到我的开关并在出现错误之前中断

Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
at Garden.fileReader(Garden.java:141)
at Garden.<init>(Garden.java:28)
at Garden.<clinit>(Garden.java:10)'

在运行 switch 语句然后进入一个单独的类来构造对象后发生错误,当返回并点击中断时,会弹出错误

public class Garden {
    public static final Garden GARDEN = new Garden();   //line 10------------
    public static void main(String[] args) {


        int mainI = 0;
        while (mainI != 100) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
            GARDEN.anotherDay();
            mainI++;
        }
    }
    static HashMap<String, Hive> HiveMap = new HashMap<String, Hive>();

    private Garden() {

        fileReader();   //line 28 --------------------------
        System.out.println("fileReader worked");
    }



    protected void fileReader() { // asks for file name for config file

        //removed try catch code that uses Scanner to get input from console
        // to select a file that is set to configFile

        Scanner configScanner = new Scanner(configFile);
        int k = 0;

        while (configScanner.hasNextLine() == true) {
            String inputLine = configScanner.nextLine();
            //removed long if statment to set k

            switch (k) {
            case 1:
                intFinder(k, inputLine);
                Hive hive = new Hive(honeyInput, pollenInput, royalJellyInput);
                HiveMap.put("hive" + hiveName, hive); line 141-------------
                break; // it gets to this break then throws the error

                // removed code
            default:
                break;
            }
        }
        cmdReader.close();
        configScanner.close();
    }

hive 的构造函数是

protected Hive(int honeyStart, int royalJellyStart, int pollenStart)
{
    bees = new ArrayList<Bee>();
    this.setHoney(honeyStart);
    this.setRoyalJelly(royalJellyStart);
    this.setPollen(pollenStart);
}

很抱歉发布了这么多代码,但我对问题的唯一想法是当代码在另一个类中运行时 configScanner 丢失数据,但事实并非如此,所以我不知道出了什么问题,任何帮助将不胜感激

最佳答案

这是因为在 Garden GARDEN 的初始化程序运行时,HiveMap 尚未初始化。将初始化 HiveMap 的行移至 Garden GARDEN 之前的一行以解决问题:

static HashMap<String, Hive> HiveMap = new HashMap<String, Hive>();
public static final Garden GARDEN = new Garden();

解决此问题的原因是静态初始化程序按文本顺序运行。 Garden() 构造函数假定 HiveMap 为非空,因为它尝试将数据放入其中:

HiveMap.put("hive" + hiveName, hive);

关于在开关内构造对象时创建 java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481661/

相关文章:

javascript - 如何通过 linkText/linkName 严格地在表的隐藏行之一中找到特定链接(不使用 css-selectors 或 xpath)?

Java 编译 - 有没有办法告诉编译器忽略我的部分代码?

c - 嵌套案例、for 和 if 循环的优先级是什么?

ios - Switch 语句嵌套格式 - 哪个是正确的?

string - 为什么 String 的 if-else 比 enum 的 switch-case 快?

java - Tomcat限制http session ,内存不足

java - 如何在没有数据库的情况下在Spring Boot中创建对象集合?

c# - 如何检测并在外部子流程调用错误对话框时使用react

java - Android Studio 上的待办事项列表-无法解决方法

xcode - 错误: logging directory does not exist/var/mobile/Library/Logs/CrashReporter/DiagnosticLog/