2014-10-28 29 views
0

由于NullPointerException的原因,我的贵班级提起ExceptioninInitializerError如何从Enum获取ImageIcon?

我的问题是,如何从我的枚举中获取信息(例如Rock的ImageIcon),并使用它来设置我的JPanel的setIcon而不会出现此错误。

编辑:当我点击GUI上的岩石按钮,这是长大

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError 
at rockPaperScissors.RockPaperScissorsGui$2.actionPerformed(RockPaperScissorsGui.java:156) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$400(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: java.lang.NullPointerException 
at javax.swing.ImageIcon.<init>(Unknown Source) 
at rockPaperScissors.RockPaperScissors.<clinit>(RockPaperScissors.java:7) 
... 37 more 

枚举

package rockPaperScissors; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 


public enum RockPaperScissors { 
    ROCK(new ImageIcon(RockPaperScissors.class.getResource("Rock.jpg"))), 
    PAPER(new ImageIcon(RockPaperScissors.class.getResource("Paper.gif"))), 
    SCISSORS(new ImageIcon(RockPaperScissors.class.getResource("Scissors.jpg"))); 

    private ImageIcon icon; 
    private int humanScore; 
    private int computerScore; 


    private RockPaperScissors(ImageIcon icon) { 
     setIcon(icon); 
    } 

    public String evaluate(int humanChoice, int computerChoice) { 
     if ((humanChoice == 1 && computerChoice == 2) 
       ||(humanChoice == 2 && computerChoice == 3) 
       ||(humanChoice == 3 && computerChoice == 1)) { 
      computerScore += 1; 
      return "You Lose"; 
     } else if ((humanChoice == 2 && computerChoice == 1) 
       ||(humanChoice == 3 && computerChoice == 2) 
       ||(humanChoice == 1 && computerChoice == 3)) { 
      humanScore += 1; 
      return "You Win"; 
     } else { 
      return "You Tie"; 
     } 
    } 

    public int getHumanScore() { 
     return humanScore; 
    } 

    public int getComputerScore() { 
      return computerScore; 
    } 

    public void setIcon(ImageIcon icon) { 
     this.icon = icon; 
    } 

    public ImageIcon getIcon() { 
     return icon; 
    } 
} 

package rockPaperScissors; 


import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 

import java.awt.GridLayout; 

import javax.swing.BoxLayout; 

import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Insets; 

import javax.swing.ImageIcon; 

import java.awt.Dimension; 
import java.awt.Component; 

import javax.swing.Box; 
import javax.swing.JLabel; 

import java.awt.Font; 

import javax.swing.SwingConstants; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.util.ArrayList; 
import java.util.Random; 
import java.awt.FlowLayout; 
import javax.swing.JTextPane; 


public class RockPaperScissorsGui extends JFrame { 

private JPanel contentPane; 

/** 
* Launch the application. 
*/ 
private int humanChoice; 
private int computerChoice; 
private Random rand = new Random(); 
private ArrayList choices = new ArrayList<>(); 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       RockPaperScissorsGui frame = new RockPaperScissorsGui(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public RockPaperScissorsGui() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 801, 525); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 

    ImageIcon background = new ImageIcon(RockPaperScissorsGui.class.getResource("Background.jpg")); 


    choices.add(1); 
    choices.add(2); 
    choices.add(3); 

    JLabel lblRockPaperScissors = new JLabel("Rock, Paper, Scissors"); 
    lblRockPaperScissors.setPreferredSize(new Dimension(103, 50)); 
    lblRockPaperScissors.setHorizontalAlignment(SwingConstants.CENTER); 
    lblRockPaperScissors.setAlignmentX(Component.CENTER_ALIGNMENT); 
    lblRockPaperScissors.setFont(new Font("Comic Sans MS", Font.PLAIN, 22)); 
    contentPane.add(lblRockPaperScissors, BorderLayout.NORTH); 

    JPanel panel = new JPanel(); 
    contentPane.add(panel, BorderLayout.SOUTH); 



    JPanel panel_1 = new JPanel(); 
    contentPane.add(panel_1, BorderLayout.CENTER); 
    panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

    Component horizontalStrut_2 = Box.createHorizontalStrut(20); 
    horizontalStrut_2.setPreferredSize(new Dimension(110, 0)); 
    panel_1.add(horizontalStrut_2); 

    JLabel userChoiceLabel = new JLabel(""); 
    userChoiceLabel.setPreferredSize(new Dimension(200, 200)); 
    panel_1.add(userChoiceLabel); 

    Component horizontalStrut = Box.createHorizontalStrut(20); 
    horizontalStrut.setPreferredSize(new Dimension(100, 0)); 
    panel_1.add(horizontalStrut); 

    JLabel computerChoiceLabel = new JLabel(""); 
    computerChoiceLabel.setPreferredSize(new Dimension(200, 200)); 
    panel_1.add(computerChoiceLabel); 

    Component horizontalStrut_1 = Box.createHorizontalStrut(20); 
    horizontalStrut_1.setPreferredSize(new Dimension(110, 0)); 
    panel_1.add(horizontalStrut_1); 

    Component horizontalStrut_4 = Box.createHorizontalStrut(20); 
    horizontalStrut_4.setPreferredSize(new Dimension(740, 60)); 
    panel_1.add(horizontalStrut_4); 

    JLabel lblUserScore = new JLabel("User Score"); 
    panel_1.add(lblUserScore); 

    JTextPane txtpnUserScore = new JTextPane(); 
    txtpnUserScore.setText("User Score"); 
    panel_1.add(txtpnUserScore); 

    Component horizontalStrut_3 = Box.createHorizontalStrut(20); 
    horizontalStrut_3.setPreferredSize(new Dimension(100, 0)); 
    panel_1.add(horizontalStrut_3); 

    JTextPane textPane = new JTextPane(); 
    panel_1.add(textPane); 

    Component horizontalStrut_5 = Box.createHorizontalStrut(20); 
    horizontalStrut_5.setPreferredSize(new Dimension(100, 0)); 
    panel_1.add(horizontalStrut_5); 

    JLabel lblComputerScore = new JLabel("Computer Score"); 
    panel_1.add(lblComputerScore); 

    JTextPane txtpnComputerScore = new JTextPane(); 
    txtpnComputerScore.setText("Computer Score"); 
    panel_1.add(txtpnComputerScore); 

    JPanel panel_2 = new JPanel(); 
    contentPane.add(panel_2, BorderLayout.WEST); 

    JPanel panel_3 = new JPanel(); 
    contentPane.add(panel_3, BorderLayout.EAST); 

    JButton btnRock = new JButton("Rock"); 
    btnRock.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 

      userChoiceLabel.setIcon(RockPaperScissors.ROCK.getIcon()); 
     } 
    }); 
    btnRock.setFont(new Font("Georgia", Font.PLAIN, 20)); 
    panel.add(btnRock); 

    JButton btnPaper = new JButton("Paper"); 
    btnPaper.setFont(new Font("Georgia", Font.PLAIN, 20)); 
    panel.add(btnPaper); 

    JButton btnScissors = new JButton("Scissors"); 
    btnScissors.setFont(new Font("Georgia", Font.PLAIN, 20)); 
    panel.add(btnScissors); 
} 

public int getHumanChoice() { 
    return humanChoice; 
} 

public int getComputerChoice() { 
    return computerChoice; 
} 

public void setComputerChoice() { 
    computerChoice = rand.nextInt(choices.size()); 
    System.out.println(computerChoice); 
} 
+0

你应该找出什么是'null'。然后你可以“没有错误地获取信息” – 2014-10-28 17:44:18

+1

它试图加载的文件是否存在?他们必须与您的项目的根位于同一位置。 – Bartvbl 2014-10-28 17:44:22

+0

'getResource'找不到您的图片。 – Radiodef 2014-10-28 18:01:06

回答

0

错误您的程序崩溃,因为RockPaperScissors.class.getResource("Rock.jpg")无法找到指定的文件。这也会计入您尝试加载的其他图片。当您将您的程序变成可运行的JAR时,Java会尝试在项目的根文件夹或与JAR文件相同的目录中查找它们。正如您所指出的那样,它们与枚举位于同一个文件夹中。

我建议在项目的根目录中创建一个名为“res”的新文件夹,并将所有图像文件移动到该文件夹​​中。接下来,替换:

new ImageIcon(RockPaperScissors.class.getResource("Rock.jpg")) 

由:

new ImageIcon("res/Rock.jpg") 

,做,对于每一个地方你加载图像的地方。

在一个不相关的说明中,由于RockPaperScissors枚举的图标不应该改变,因此使它们公开为final是更好(也更容易)。最终变量只能分配一次,并且必须在构建对象时分配。因此它们是常数。代码:

public final ImageIcon icon; 

private RockPaperScissors(ImageIcon icon) { 
    this.icon = icon; 
} 

而只是摆脱getter和setter,你现在可以直接进入图标字段。