当我运行我的代码时,我得到这个错误:
线程“主”java.lang.NullPointerException 中的异常
at Main.drawBlock(Main.java:48)
at Main.<init>(Main.java:43)
at Main.main(Main.java:58)
我认为这是因为我绘制的图形,但以前从未发生过。我不知道为什么。这是我的代码:
Graphics2D g;
static JFrame jf = new JFrame();
Image Air;
Image Grass;
Image icon;
public Main() {
icon = new ImageIcon(this.getClass().getResource("Icon.png")).getImage();
Grass = new ImageIcon(this.getClass().getResource("Grass.png")).getImage();
Air = new ImageIcon(this.getClass().getResource("Air.png")).getImage();
jf.setIconImage(icon);
drawBlock(Air,0,0);
}
private void drawBlock(Image img, int x, int y) {
g.drawImage(img,x,y,null);
}
public static void main(String[] args) {
jf.setSize(792,528);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setTitle("Minecraft 2D Adventure");
new Main();
}}