2013-12-12 5 views
0

У меня возникли серьезные проблемы с моей игрой астероидов. Я пытаюсь вызвать мой метод Game.java run() в моем основном методе в Asteroid.java, но я получаю ту же ошибку:Астероид игра NullPointerException error

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(); 
    } 
} 

// NEW 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)); 
       } 


     } 
} 

// Edit Update // Хорошо, я попробовал много вещей, и обнаружил, что несмотря на то, что игра работает, когда я отладки Game.java и она не работает, когда я запускаю его через астероиды. Ява. Я обнаружил, что img = createIimg = createImage (ширина, высота); и 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 
+0

Вы получаете исключение NullPointerException в строке 15. Найдите его, затем выясните, почему он равен нулю. Я предполагаю, что это виноват: 'new Asteroids() .game.run();' – keyser

+1

'Game game = null;' –

ответ

0

Эта вещь является недействительным.

new Asteroids().game 

Именно поэтому вы получаете это исключение NullPointerException, когда вы вызываете его.

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

Программа работает от метода main. game - null.

Может быть, вы должны иметь

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

Попробуйте это может быть помочь

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

У вас есть переменная «игра» нулевая, и вы пытались вызвать метод «запустить» на этом варе (game.run); очевидно, если «игра» равна нулю, вы не можете получить этот метод и выбрасываете исключение nullpointer.

Game game=new Game(); 

это все, что вам нужно. Ваш окончательный код:

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

Я пробовал это и теперь получаю ошибку Exception в потоке «main» java.lang. NullPointerException \t в asteroids.Game.run (Game.java:124) \t в asteroids.Asteroids.main (Asteroids.java:14) Java результат: 1 – user3096529

+0

Очень проще говоря, NullPointerException почти гарантированно быть результатом вы пытаетесь вызвать метод на объекте, который, по вашему мнению, был реальным, но оказался нулевым. Первоначально было очевидно, что на линии 'new Asteroids().game.run() 'это была' игра', которая была нулевой; теперь, я предлагаю вам внимательно изучить строку 124 Game.java и выяснить, что может быть пустым в этой строке. Вот подсказка: вы никогда не называете 'init()' на вашей 'Game'. – dcsohl

+0

Хорошо, я пробовал много вещей и обнаружил, что даже если игра работает, когда я отлаживаю Game.java, и она не работает, когда я запускаю ее через Asteroids.java. Я обнаружил, что img = createIimg = createImage (ширина, высота); и g = img.getGraphics(); возвращают значение null и что GraphicsEnvironment.isHeadless() возвращает true. Как мне изменить свою проблему, чтобы исправить эту проблему? – user3096529

Смежные вопросы