简体   繁体   English

Java:为枚举获取ExceptionInInitializerError

[英]Java: Getting ExceptionInInitializerError for Enums

EDIT: Solved but I don't understand why 编辑:解决了,但是我不明白为什么

In PokemonEnum I had this line 在PokemonEnum中,我有这条线

private PokemonEnum[ ] pokemon = PokemonEnum.values(); 

I changed it to: 我将其更改为:

private static PokemonEnum[ ] pokemon = PokemonEnum.values(); 

and now it works. 现在可以了。 I never even used that array yet so I don't know why I was getting errors or why static fixed it. 我什至从未使用过该数组,所以我不知道为什么会出错或为什么静态要修复它。


I haven't really worked with Enums so I don't really know why I get an ExceptionInInitializerError when I run main (on line 28 when I try to create a new Pokemon). 我还没有真正与Enums一起工作,所以我真的不知道为什么在运行main时为什么会遇到ExceptionInInitializerError(在尝试创建新的Pokemon时,出现在第28行)。 Anyone care to explain please? 有人在乎解释吗? Thanks. 谢谢。

import java.awt.image.BufferedImage;
import java.util.ArrayList;


public class Pokemon {

    private PokemonEnum name;
    private int dexNumber;
    private BufferedImage sprite;
    private TypeEnum[] types = new TypeEnum[1];
    private ArrayList<AbilityEnum> abilities;
    private ArrayList<MoveEnum> moves;
    private short hp;
    private short attack;
    private short defense;
    private short special_attack;
    private short special_defense;
    private short speed;

    public Pokemon(PokemonEnum name)
    {
        this.name = name;
        this.dexNumber = name.getDexNum();
    }

    public static void main(String[] args)
    {
        Pokemon pikachu = new Pokemon(PokemonEnum.Pikachu);
        System.out.println(pikachu.dexNumber);
    }
}



   public enum PokemonEnum {
    Pikachu;

    public int getDexNum()
    {
        return ordinal()+1;
    }

    private PokemonEnum[ ] pokemon = PokemonEnum.values(); 
}


Stack Trace:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at Pokemon.main(Pokemon.java:28)
Caused by: java.lang.NullPointerException
    at PokemonEnum.values(PokemonEnum.java:1)
    at PokemonEnum.<init>(PokemonEnum.java:722)
    at PokemonEnum.<clinit>(PokemonEnum.java:2)
    ... 1 more

What you are experiencing is 'like' a recursion. 您所经历的就像“递归”。

This error occurs because the code PokemonEnum.values() is in the enum PokemonEnum and when this is compiling it reads values() and then using this internal function, the primitive data type enum references to itself. 发生此错误的原因是,代码PokemonEnum.values()位于枚举PokemonEnum并且在编译时先读取values() ,然后使用此内部函数,原始数据类型enum引用了自身。 However due to the fact that the enum is still being compiled, the value of value() is null . 但是,由于该enum仍在编译中,因此value()值为null

Note: Trying to use the value() inside of it's enum will result in an Error. 注意:尝试在枚举内部使用value()会导致错误。 Such that trying to use if(PokemonEnum.values()!=null) or even trying to catch the ExceptionInInitializerError will not work due to the values() being part of a primitive type( meaning the method being called is native ). 由于values()是原始类型的一部分,因此尝试使用if(PokemonEnum.values()!=null)甚至试图捕获ExceptionInInitializerError都不会起作用( 这意味着被调用的方法是native )。

The Solution would be to place private PokemonEnum[] pokemon = PokemonEnum.values(); 解决方案是放置private PokemonEnum[] pokemon = PokemonEnum.values(); outside and below the enum PokemonEnum . 在枚举PokemonEnum外部和下方。

I know this from personal experience as well as other sources on similar problems. 我从个人经验以及类似问题的其他来源了解到这一点。

Similar Sources: here and here 相似来源: 这里这里

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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