爱问知识人 爱问教育 医院库

java 抛出异常

首页

java 抛出异常

public class MyFrame extends JFrame implements KeyListener {
  private List<BackGround> allBG=new ArrayList<BackGround>();
    private BackGround nowBG;
    public MyFrame(){
      this.setTitle("");
      this.setSize(900, 600);
      int width=Toolkit.getDefaultToolkit().getScreenSize().width;
      int height=Toolkit.getDefaultToolkit().getScreenSize().height;
      this.setLocation(200, 100);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setVisible(true);
      this.setResizable(false);
      
      //初始化全部的图片
      StaticValue.init();
   //创建所有的场景
   for(int i=1;i<=3;i++)
   {this.allBG.add(new BackGround(i,i==3? ));}
   //将第一个场景设置为当前场景
   this.nowBG=this.allBG.get(0);
      }
    public void paint(Graphics g)
    {
      //建立一张临时的缓冲图片
      BufferedImage image=new BufferedImage(900,600,BufferedImage.TYPE_3BYTE_BGR);
      Graphics g2=image.getGraphics();
      //绘制背景图片
      g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
        //把缓冲图片绘制到窗体中
      g.drawImage(image,0,0,this);}

运行程序后 窗口显示了我的当前背景图片 但是抛出这样的异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at test.MyFrame.paint(MyFrame. )问题出在这g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);

提交回答

全部答案

    2012-11-24 18:07:25
  • 你吧this.nowBG.getBgImage()赋给一个临时变量,
    打印出来看看他是什么?
    是不是NULL
    如果是很有可能是你当前没有定义背景造成的

    好***

    2012-11-24 18:07:25

类似问题

换一换
  • 上网帮助 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):