线程“Thread-2”中的异常java.lang.NullPointerException Games Flappy

时间:2017-07-02 11:45:23

标签: java eclipse image nullpointerexception 2d

我试图为我的作业制作飞扬的鸟,但我得到了这样的错误

  

线程“Thread-2”中的异常java.lang.NullPointerException
  在main.Main.update(Main.java:84)
  在main.Main.run(Main.java:64)
  在java.lang.Thread.run(未知来源)

它表示我的bird.update()为空或其他东西,但我无法解决它。

我试图在鸟类中将图像更改为矩形并且它正在工作,但我需要将图像用于我的作业

这是我的主类

的代码
package main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import infra.ControlSystem;

import javax.imageio.ImageIO;
public class Main extends Canvas implements Runnable,KeyListener{

    /**
     * 
     */
    private static final long serialVersionUID = 3196520274556633778L;
    public static final int WIDTH = 800, HEIGHT = 600;
    private Thread thread;
    private Random r;
    private Tube tube;
    private Bird bird;

    private boolean running = false;
    public Main(){
      new Window(WIDTH,HEIGHT,"Altenative Bird", this);
      r = new Random();
      addKeyListener(this);
      tube = new Tube(80);
      BufferedImage sheets = null;
      try {
          sheets = ImageIO.read(getClass().getResource("/twitter.png"));

      } catch (IOException e) {
           // TODO Auto-generated catch block
            e.printStackTrace();
      }
      if(sheets == null){
           System.out.println("damn it's null");
      }else{
           System.out.println("hi");
      }
      bird = new Bird(100,Main.HEIGHT/2,tube.tubes,sheets);
    }  
    public synchronized void start(){
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop(){
        try{
                thread.join();
                running = false;
        }catch(Exception e){
                e.printStackTrace();
        }
    }
    @Override
    public void run(){
        int frames = 0;
        long lastTime = System.nanoTime();
        double amountOFTicks = 60.0;
        double ns = 1000000000 / amountOFTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        while(running){
            long now  =System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >=1 ){
                update();
                render();
                    // TODO Auto-generated catch block

                delta--;
                frames++;

            }

            if(System.currentTimeMillis() - timer>=1000){
                timer+=1000;
                System.out.println("FPS: "+frames);
                frames=0;
            }
        }
    }

    private void update(){
        tube.update();
        System.out.println(bird);
        bird.update();
    }

    private void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            this.createBufferStrategy(3);
            return;
        }
        sheets = ImageIO.read(getClass().getResource("/twitter.png"));
        texture[0] = sheets.getSubimage(0, 0, 150, 150);
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        //BufferedImage bi = ImageIO.read(new File("Res//forest.png"));
        //g.drawImage(bi, 0, 0, getWidth(), getHeight(), this);

        tube.render(g);
        bird.render(g);
        g.dispose();
        bs.show();


    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode()==KeyEvent.VK_SPACE){
            bird.pressed = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode()==KeyEvent.VK_SPACE){
            bird.pressed = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main();
    }

}

这是我的鸟类的代码

    package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt. Rectangle;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class Bird extends Rectangle{
    private int spd=10;
    public boolean pressed=false;
    private BufferedImage sheets;
    private BufferedImage[] texture;
    private ArrayList<Rectangle> tubes;
    public Bird(int x,int y,ArrayList<Rectangle> tubes,BufferedImage sheets){  
        setBounds(x,y,32,23);
        this.tubes = tubes;
        texture = new BufferedImage[4];
            texture[0] = sheets.getSubimage(0, 0, 150, 150);
            texture[1] = sheets.getSubimage(150, 0, 150, 150);
            texture[2] = sheets.getSubimage(300, 0, 150, 150);
            texture[3] = sheets.getSubimage(450, 0, 150, 150);
    }
    public void update(){
        if(pressed){
            y-=spd;
        }else{
            y+=spd; 
        }
            for(int i=0;i<tubes.size();i++){    
                if(this.intersects(tubes.get(i))){  
                    System.exit(0);
                }
            }
            if(y==550){
                System.exit(0);   
            }
    }

    public void render(Graphics g){
        //g.setColor(Color.gray);
        g.drawImage(texture[0], x, y, width, height, null);
        //g.fillRect(x, y, width, height);

    }
}

提前致谢

0 个答案:

没有答案