Java swing中主类中new 主类对象时出现NullPointerException

public class Main extends JPanel{
    public Hero hero=new Hero();
    public static void main(String[] args) {
        JFrame jfr=new JFrame("飞机大战");
        System.err.println("===========================================");
        Main jpan=new Main();//main是个画板 <------------------问题出现在这里
        jfr.add(jpan);
        jfr.setSize(WIDTH,HEIGHT);
        jfr.setAlwaysOnTop(true);
        jfr.setLocationRelativeTo(null);
        jfr.setResizable(false);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jpan.action();//界面动态化
        jfr.setVisible(true);//显示-尽快调用paint方法
    }        
}
//下面的时Hero类
public class Hero extends FlyObject{
    BufferedImage [] images;
    private int life;
    private int fire=0;
    private int score=0;
    private int index=0;
    
    /**初始化英雄机*/
    public Hero(){//<---------------------构造方法
        image=Main.hero1;
        images=new BufferedImage[]{Main.hero1,Main.hero2};
        w=image.getWidth();
        h=image.getHeight();
        this.fire=0;
        this.life=3;
        this.x=150;
        this.y=400;
    }
//下面的是Hero类的父类    
public abstract class FlyObject {
    protected int h;
    protected int w;
    protected int x;
    protected int y;
    protected BufferedImage image;
    
    /**获取移动后得坐标*/
    public abstract void move();
    /**判断当前移动单位是否存活*/
    public abstract boolean live(FlyObject fo);
}

本人了解空指针异常的原因是因为对象的地址为空,也就是没有实例化
图片描述下么的是异常提醒

阅读 2.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题