由於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);
}
你應該找出什麼是'null'。然後你可以「沒有錯誤地獲取信息」 – 2014-10-28 17:44:18
它試圖加載的文件是否存在?他們必須與您的項目的根位於同一位置。 – Bartvbl 2014-10-28 17:44:22
'getResource'找不到您的圖片。 – Radiodef 2014-10-28 18:01:06