小行星游戏NullPointerException错误

时间:2013-12-12 18:02:17

标签: java netbeans nullpointerexception

我的小行星游戏遇到了一些严重的问题。我试图在run()的主方法中调用我的Game.java Asteroid.java方法,但我一直得到同样的错误:

Exception in thread "main" java.lang.NullPointerException
at asteroids.Asteroids.main(Asteroids.java:15)
Java Result: 1

有人可以帮我弄清楚为什么会这样吗?

这是我的代码:

Asteroids.java

package asteroids;
import java.applet.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.io.IOException; 

@SuppressWarnings("serial")
public class Asteroids {

    Game game = null;
    public static void main(String[] args){
        new Asteroids ().game.run ();
    }
}

// 新Game.java //

包裹小行星;

import java.applet.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;

//@SuppressWarnings("serial")
public class Game extends Applet implements Runnable, KeyListener {

        //timing variables
        Thread thread;
        long startTime, endTime, framePeriod;

        //graphics variables
        Image img;
        Dimension dim;
        int width, height;
        Graphics g;

        //text items
        int level, lives, score;

        SpaceShip ship;
        boolean shipCollision, shipExplode;

        //ArrayList to hold asteroids
        ArrayList<Asteroid> asteroids = new ArrayList<>();
        int numOfAsteroids = 1;

        //ArrayList to hold the lasers
        ArrayList<Laser> lasers = new ArrayList<>();
        final double rateOfFire = 10; //limits rate of fire
        double rateOfFireRemaining; //decrements rate of fire

        //ArrayList to hold explosion particles
        ArrayList<AsteroidExplosion> explodingLines = new ArrayList<>();

        //ArrayList to hold ship explosions
        ArrayList<ShipExplosion> shipExplosion = new ArrayList<>();
       public void Game ()
       {
           init();

       }
        public void init() {
                resize(900,700); //set size of the applet
                dim = getSize(); //get dimension of the applet
                width = dim.width;
                height = dim.height;
                framePeriod = 25; //set refresh rate

                addKeyListener(this); //to get commands from keyboard
                setFocusable(true);

                ship = new SpaceShip(width/2, height/2, 0, .15, .5, .15, .98); //add ship to game
                shipCollision = false;
                shipExplode = false;
                level = numOfAsteroids;
                lives = 3;
                addAsteroids();


                img = createImage(width, height); //create an off-screen image for double-buffering
                g = img.getGraphics(); //assign the off-screen image
                thread = new Thread(this);
                thread.start();
        }

        @Override
        public void paint(Graphics gfx) {
                Graphics2D g2d = (Graphics2D) g;
        //give the graphics smooth edges
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHints(rh);

        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, width, height); //add a black background

        //add text for lives, score, and level
        g2d.setColor(Color.WHITE);
        g2d.drawString("Level : " + level, 10, 690);
        g2d.drawString("Lives : " + lives, 110, 690);
        g2d.drawString("Score : " + score, 210, 690);



        for(Asteroid a: asteroids) { //draw asteroids
                a.draw(g2d);
        }

        for(Laser l : lasers) { //draw lasers
                l.draw(g2d);
        }

        for(AsteroidExplosion e : explodingLines) {
                e.draw(g2d);
        }

        for(ShipExplosion ex : shipExplosion)
                ex.draw(g2d);

        ship.draw(g2d); //draw ship
        if(shipCollision) {
                shipExplosion.add(new ShipExplosion(ship.getX(), ship.getY(), 10, 10));
                        ship.setX(width/2);
                        ship.setY(height/2);
                        shipCollision = false;
                        lives--;
        }

        gfx.drawImage(img, 0, 0, this); //draw the off-screen image (double-buffering) onto the applet
        }

        @Override
        public void update(Graphics gfx) {
                paint(gfx); //gets rid of white flickering
        }

        @Override
        public void run(){
                for( ; ; ) {
                        startTime = System.currentTimeMillis(); //timestamp
                        ship.move(width, height);                       //ship movement
                        for(Asteroid a : asteroids) {           //asteroid movement
                                a.move(width, height);
                        }
                        for(Laser l : lasers) {                         //laser movement
                                l.move(width, height);
                        }
                        for(int i = 0 ; i<lasers.size() ; i++) {        //laser removal
                                if(!lasers.get(i).getActive())
                                        lasers.remove(i);
                        }
                        for(AsteroidExplosion e : explodingLines) {                     //asteroid explosion floating lines movement
                                e.move();
                        }
                        for(int i = 0 ; i<explodingLines.size(); i++) {         //asteroid explosion floating lines removal
                                if(explodingLines.get(i).getLifeLeft() <= 0)
                                        explodingLines.remove(i);
                        }
                        for(ShipExplosion ex : shipExplosion){  //ship explosion expansion
                                ex.expand();
                        }
                        for(int i = 0 ; i<shipExplosion.size() ; i++) {        
                                if(shipExplosion.get(i).getLifeLeft() <= 0)
                                        shipExplosion.remove(i);
                        }
                        rateOfFireRemaining--;
                        collisionCheck();
                        if(asteroids.isEmpty()) {
                                numOfAsteroids++;
                                addAsteroids();
                                level = numOfAsteroids;
                        }
                        repaint();
                        try {
                                endTime = System.currentTimeMillis(); //new timestamp
                                if(framePeriod - (endTime-startTime) > 0)                               //if there is time left over after repaint, then sleep
                                        Thread.sleep(framePeriod - (endTime - startTime));      //for whatever is  remaining in framePeriod
                        } catch(InterruptedException e) {}
                }
        }

        @Override
        public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                //fires laser
                if(key == KeyEvent.VK_SPACE) {
                        if(rateOfFireRemaining <= 0 ) {
                                lasers.add(ship.fire());
                                rateOfFireRemaining = rateOfFire;
                        }
                }
                if(key == KeyEvent.VK_UP)
                        ship.setAccelerating(true);
                if(key == KeyEvent.VK_RIGHT)
                        ship.setTurningRight(true);
                if(key == KeyEvent.VK_LEFT)
                        ship.setTurningLeft(true);
                if(key == KeyEvent.VK_DOWN)
                        ship.setDecelerating(true);
        }

        @Override
        public void keyReleased(KeyEvent e) {
                int key = e.getKeyCode();
                if(key == KeyEvent.VK_UP)
                        ship.setAccelerating(false);
                if(key == KeyEvent.VK_RIGHT)
                        ship.setTurningRight(false);
                if(key == KeyEvent.VK_LEFT)
                        ship.setTurningLeft(false);
                if(key == KeyEvent.VK_DOWN)
                        ship.setDecelerating(false);
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

        public void addAsteroids() {
                int numAsteroidsLeft = numOfAsteroids;
                int size;

                for(int i=0 ; i<numOfAsteroids ; i++) {         //add asteroids to game
                        //randomize starting position
                        int asteroidX = (int)(Math.random() * width) + 1;
                        int asteroidY = (int)(Math.random() * height) + 1;

                        //randomize speed and direction
                        double xVelocity = Math.random() + 1; //horizontal velocity
                    double yVelocity = Math.random() + 1; //vertical velocity
                    //used starting direction
                    int xDirection = (int)(Math.random() * 2);
                    int yDirection = (int)(Math.random() * 2);
                        //randomize horizontal direction
                        if (xDirection == 1)
                                xVelocity *= (-1);
                        //randomize vertical direction
                        if (yDirection == 1)
                                yVelocity *= (-1);

                    //if there are more then four asteroids, any new ones are MEGA asteroids
                    if(numAsteroidsLeft > 4) {
                        size = 2;
                    } else { size = 1;
                    }

                        asteroids.add(new Asteroid(size, asteroidX, asteroidY, 0, .1, xVelocity, yVelocity));
                        numAsteroidsLeft--;

                        //Make sure that no asteroids can appear right on top of the ship
                                //get center of recently created asteroid and ship and check the distance between them
                                Point2D asteroidCenter = asteroids.get(i).getCenter();
                                Point2D shipCenter = ship.getCenter();
                                double distanceBetween = asteroidCenter.distance(shipCenter);

                                //if asteroid center is within 80 pixels of ship's center, remove it from the ArrayList and rebuild it
                                if(distanceBetween <= 80) {
                                        asteroids.remove(i);
                                        i--;
                                        numAsteroidsLeft++;
                                }

                }
        }

        public void collisionCheck() {
                //cycle through active asteroids checking for collisions
                for(int i = 0 ; i < asteroids.size() ; i++) {
                        Asteroid a = asteroids.get(i);
                        Point2D aCenter = a.getCenter();

                        //check for collisions between lasers and asteroids
                        for(int j = 0 ; j < lasers.size() ; j++) {
                                Laser l = lasers.get(j);
                                Point2D lCenter = l.getCenter();

                                double distanceBetween = aCenter.distance(lCenter);
                                if(distanceBetween <= (a.getRadius() + l.getRadius())) {

                                        //split larger asteroids into smaller ones, remove smaller asteroids from screen
                                        if(a.getRadius() >= 60) {
                                                for(int k = 0 ; k < 3 ; k++)
                                                        explodingLines.add(a.explode());
                                                split(i);
                                                score += 200;
                                        } else if(a.getRadius() >= 30){
                                                for(int k = 0 ; k < 3 ; k++)
                                                        explodingLines.add(a.explode());
                                                split(i);
                                                score += 100;
                                        } else {
                                                for(int k = 0 ; k < 3 ; k++)
                                                        explodingLines.add(a.explode());
                                                asteroids.remove(i);
                                                score += 50;
                                        }

                                        lasers.remove(j); //remove laser from screen
                                }
                        }

                        //check for collisions between ship and asteroid
                        Point2D sCenter = ship.getCenter();
                        double distanceBetween = aCenter.distance(sCenter);
                        if(distanceBetween <= (a.getRadius() + ship.getRadius())) {
                                shipCollision = true;
                                shipExplode = true;
                        }
                }
        }

        public void split(int i) {
                Asteroid a = asteroids.get(i);
                double bigAsteroidX = a.getX();
                double bigAsteroidY = a.getY();
                int size = (a.getSize() / 2);
                asteroids.remove(i);
                for(int j = 0 ; j<2 ; j++) {
                        //randomize speed and direction
                        double xVelocity = Math.random() + 1; //horizontal velocity
                    double yVelocity = Math.random() + 1; //vertical velocity
                    //used randomize starting direction
                    int xDirection = (int)(Math.random() * 2);
                    int yDirection = (int)(Math.random() * 2);
                        //randomize horizontal direction
                        if (xDirection == 1)
                                xVelocity *= (-1);
                        //randomize vertical direction
                        if (yDirection == 1)
                                yVelocity *= (-1);
                        asteroids.add(new Asteroid(size, bigAsteroidX, bigAsteroidY, 0, .1, xVelocity, yVelocity));
                }


        }
}

// 修改更新 // 好吧,我尝试了很多东西,并发现即使游戏在调试Game.java时有效,但当我通过Asteroids.java运行它时它也不起作用。我发现img = createIimg = createImage(width,height);和g = img.getGraphics();返回null并且GraphicsEnvironment.isHeadless()返回true。我应该如何改变我来修复这个问题?

错误

Exception in thread "main" java.lang.NullPointerException
    at asteroids.Game.init(Game.java:67)
    at asteroids.Game.Game(Game.java:45)
    at asteroids.Asteroids.main(Asteroids.java:15)
Java Result: 1

4 个答案:

答案 0 :(得分:1)

你有var“game”null,你试图在这个var(game.run)上调用方法“run”;很明显,如果“game”为null,则无法获取该方法,并抛出nullpointer异常。

Game game=new Game();

这就是你所需要的一切。您的最终代码是:

Game game = new Game();//<----- here is the change
public static void main(String[] args){
    new Asteroids ().game.run ();
}

答案 1 :(得分:0)

这件事是空的。

new Asteroids ().game

这就是为什么你在它上面调用run时会得到这个NullPointerException。

答案 2 :(得分:0)

Game game = null;
public static void main(String[] args){
    new Asteroids ().game.run ();        
}

该程序从main方法运行。 game为空。

也许你应该

Game game = new Game();  // instead of null

答案 3 :(得分:0)

尝试一下可能会有所帮助

Game game = null;
public static void main(String[] args){
If( Asteroids ().game != null ){
    new Asteroids ().game.run (); 
}       
}