我知道這是很簡單的,但因爲我已經實例化的類,我不明白爲什麼我得到這個異常:當我嘗試運行我的JFrame時,爲什麼會出現NullPointerException?
異常線程「main」顯示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();
}
}
我試過做所有的構造函數(例如setVisible(true))在main()中,但我得到相同的錯誤。令人驚訝的是,這方面沒有太多容易找到的信息。爲什麼imageIcon給它一個問題?謝謝你們的幫助!
你從哪裏得到的NPE? – JHS
_easily找到info_是在您的錯誤消息。發佈完整的堆棧跟蹤。 – jlordo
我猜你不需要文件名的前導「/」。 – camickr