我知道这真的很简单,但是由于我已经实例化了这个类,我不明白为什么会出现这个异常:
线程“主”java.lang.NullPointerException 中的异常
在 javax.swing.ImageIcon.(ImageIcon.java:181)
在 GameFrame.(GameFrame.java:16)
在 GameFrame.main(GameFrame.java:88)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameFrame extends JFrame implements ActionListener{
//Mini games and main panel components
private JPanel MainPanel;
private JPanel gamePanel1, gamePanel2, gamePanel3, gamePanel4, gamePanel5, gamePanel6, gamePanel7, gamePanel8, gamePanel9;
private JPanel[] gamePanels = {gamePanel1, gamePanel2, gamePanel3, gamePanel4, gamePanel5, gamePanel6, gamePanel7, gamePanel8, gamePanel9};
private JButton[][] buttons;
private int turn;
//X and O images
private JLabel X = new JLabel();
private ImageIcon x = new ImageIcon(getClass().getResource("/Images/X.PNG"));
private JLabel O = new JLabel();
private ImageIcon o = new ImageIcon(getClass().getResource("/Images/O.PNG"));
//constructor
public GameFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
//gf.setSize(800,600);
setVisible(true);
setUpMainPanel();
add(MainPanel);
}
public void setUpMainPanel() {
//sets the layout
}
public static void main(String[] args) {
GameFrame frame = new GameFrame();
}
}
我已经尝试在 main() 中执行所有构造函数(例如 setVisible(true)),但我得到了同样的错误。令人惊讶的是,在这方面没有很多容易找到的信息。为什么 imageIcon 给它一个问题?谢谢你们的帮助!