我创建了我的 java 游戏的 .jar 文件。该程序使用 java 编译器运行良好,但是当我尝试运行 .jar 文件时,它没有显示任何结果。我通过 CMD 再次运行它:
java -jar PokemonGame.jar`
...它给了我一个错误:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(init)(ImageIcon.java:167)
at MainFile.(init)(MainFile.java:25)
at MainFile.main(MainFile.java:86)
我跟踪了我评论的行号:
public class MainFile extends JPanel implements MouseListener{
MainFile m;
static JFrame mainWindow = new JFrame("POKEMON MEMORY GAME");
static TimerFile timerPanel;
static GridFile gridPanel;
static LogsFile logsPanel;
static ButtonMenuFile buttonMenuPanel;
JPanel blockPanel;
URL url;
BufferedImage winlose;
JPanel winlosePanel;
//MainFile line 25
ImageIcon gameBackground = new ImageIcon(getClass().getResource("Assets\\Pokedex.png"));
Image gameImage = gameBackground.getImage();
GameSoundFile gameSound = new GameSoundFile();
GameSoundFile screenSound = new GameSoundFile();
//...some codes here
public static void main(String[] args) {
//MainFile line 86
MainFile mainPanel = new MainFile();
mainPanel.setMainPanel(mainPanel);
mainWindow.add(mainPanel);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.pack();
mainWindow.setLocationRelativeTo(null);
mainWindow.setResizable(false);
mainWindow.setVisible(true);
}
public MainFile(){
//...MainFile codes here
}
}
谁能告诉我这里的缺陷,因为这个程序能够使用 java 编译器运行但不能在 .jar 可执行文件上运行,这似乎是一种奇怪的行为。