加载图像时,枚举引发错误

时间:2013-10-12 22:28:51

标签: java enums

这都是Java。主要问题是在Enum类中我声明了许多不同的枚举。当声明每个声明时,会有一个图像路径告诉构造函数从何处加载图像。但是当我调用我创建的enum.getImage()方法时,它会抛出ExceptionInInitializerError。这是Enum类:

    import java.awt.Image;
import javax.swing.ImageIcon;
import java.util.HashMap;

public enum Cards {
    ACE_HEART(11,1,"acehearts.png"), ACE_SPADE(11,2,"acespades.png"), ACE_CLUB(11,3,"aceclubs.png"), ACE_DIAMOND(11,4,"acediamonds.png"),
    TWO_HEART(2,5,"twohearts.png"), TWO_SPADE(2,6,"twospades.png"), TWO_CLUB(2,7,"twoclubs.png"), TWO_DIAMOND(2,8,"twodiamonds.png"),
    THREE_HEART(3,9,"threehearts.png"), THREE_SPADE(3,10,"threespades.png"), THREE_CLUB(3,11,"threeclubs.png"), THREE_DIAMOND(3,12,"threediamonds.png"),
    FOUR_HEART(4,13,"fourhearts.png"), FOUR_SPADE(4,14,"fourspades.png"), FOUR_CLUB(4,15,"fourclubs.png"), FOUR_DIAMOND(4,16,"fourdiamonds.png"),
    FIVE_HEART(5,17,"fivehearts.png"), FIVE_SPADE(5,18,"fivespades.png"), FIVE_CLUB(5,19,"fiveclubs.png"), FIVE_DIAMOND(5,20,"fivediamonds.png"),
    SIX_HEART(6,21,"sixhearts.png"), SIX_SPADE(6,22,"sixspades.png"), SIX_CLUB(6,23,"sixclubs.png"), SIX_DIAMOND(6,24,"sixdiamonds.png"),
    SEVEN_HEART(7,25,"sevenhearts.png"), SEVEN_SPADE(7,26,"sevenspades.png"), SEVEN_CLUB(7,27,"sevenclubs.png"), SEVEN_DIAMOND(7,28,"sevendiamonds.png"),
    EIGHT_HEART(8,29,"eighthearts.png"), EIGHT_SPADE(8,30,"eightspades.png"), EIGHT_CLUB(8,31,"eightclubs.png"), EIGHT_DIAMOND(8,32,"eightdiamonds.png"),
    NINE_HEART(9,33,"ninehearts.png"), NINE_SPADE(9,34,"ninespades.png"), NINE_CLUB(9,35,"nineclubs.png"), NINE_DIAMOND(9,36,"ninediamonds.png"),
    TEN_HEART(10,37,"tenhearts.png"), TEN_SPADE(10,38,"tenspades.png"), TEN_CLUB(10,39,"tenclubs.png"), TEN_DIAMOND(10,40,"tendiamonds.png"),
    JACK_HEART(10,41,"jackhearts.png"), JACK_SPADE(10,42,"jackspades.png"), JACK_CLUB(10,43,"jackclubs.png"), JACK_DIAMOND(10,44,"jackdiamonds.png"),
    QUEEN_HEART(10,45,"queenhearts.png"), QUEEN_SPADE(10,46,"queenspades.png"), QUEEN_CLUB(10,47,"queenclubs.png"), QUEEN_DIAMOND(10,48,"queendiamonds.png"),
    KING_HEART(10,49,"kinghearts.png"), KING_SPADE(10,50,"kingspades.png"), KING_CLUB(10, 51, "kingclubs.png"), KING_DIAMOND(10, 52, "kingdiamonds.png");
    int value,id;
    Image image;
    private HashMap<Integer,Cards> cardmap = new HashMap<Integer,Cards>();
    public void setMap() {
        for(Cards c : Cards.values()) {
            cardmap.put(c.getValue(),c);
        }
    }
    private Cards(int value, int id, String imagepath) {
        this.value = value;
        this.id = id;
        ImageIcon i = new ImageIcon(this.getClass().getResource("img/" + imagepath));
        Image image = i.getImage();
        this.image = image;
    }
    public Image getImage() {
        return image;
    }
    public int getId() {
        return id;
    }
    public int getValue() {
        return value;
    }
    public Cards getEnum(int id) {
        return cardmap.get(id);
    }
}

如果您对错误有什么想法(不,我不是要求代码)你能解释一下我做错了吗?

编辑:完整堆栈跟踪:

    Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at blackjack.Board.paint(Board.java:22)
    at javax.swing.JComponent.paintChildren(JComponent.java:870)
    at javax.swing.JComponent.paint(JComponent.java:1046)
    at javax.swing.JComponent.paintChildren(JComponent.java:870)
    at javax.swing.JComponent.paint(JComponent.java:1046)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    at javax.swing.JComponent.paintChildren(JComponent.java:870)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5139)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1523)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1454)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1257)
    at javax.swing.JComponent.paint(JComponent.java:1023)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1778)
    at java.awt.Window.paint(Window.java:3390)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:825)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:802)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:46)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1668)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
    at blackjack.Cards.<init>(Cards.java:26)
    at blackjack.Cards.<clinit>(Cards.java:7)
    ... 40 more

编辑2: 确切地说如何设置包:

包装二十一点。 内包:二十一点类,Board类,卡类,带图像的img文件夹。

Package Explorer Snip

1 个答案:

答案 0 :(得分:0)

您的enum无法正确初始化,很可能是因为它在构造函数中抛出NullPointerException

ImageIcon i = new ImageIcon(this.getClass().getResource("img/" + imagepath));

通过上述声明,您在与img/imagepath相同的包中寻找enum。使用/替换

作为路径的前缀
ImageIcon i = new ImageIcon(this.getClass().getResource("/img/" + imagepath));

因此它相对于类路径的根,因为我假设您的文件(img包)是。

如果/img只是一个文件夹,则需要将其添加到类路径中。如果您在Eclipse上,则将其拖放到src文件夹中。