2011-10-06 100 views
0

嘿傢伙的IM一次Java遊戲,我收到以下錯誤:空指針異常?

actionPerformedException in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at OurGame.Ball.checkCollision(Ball.java:53) 
    at OurGame.Ball.actionPerformed(Ball.java:57) 
    at javax.swing.Timer.fireActionPerformed(Unknown Source) 
    at javax.swing.Timer$DoPostEvent.run(Unknown Source) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

這是發生在我的遊戲將檢查球和我的其他物體之間的碰撞。這是我對 「Ball.java」

package OurGame; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 

public class Ball extends JPanel implements ActionListener { 
/** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    int x, y, cx, cy; 

     Image img; 
     ImageIcon i; 

     Player p; 
     Board b; 

     Timer t; 
     Random r; 

     Rectangle player, ball; 


     public Ball() { 
      r = new Random(); 
      x = 500; 
      y = 190; 
      cx = -1; 


      System.out.println("New coin created: " + x + ", " +y); 

      i = new ImageIcon("ball.png"); 
      img = i.getImage(); 

      t = new Timer(10,this); 
      t.start(); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.print("actionPerformed"); 
      checkCollision(); 
      move(); 
     } 

     public void checkCollision(){ 

      player = new Rectangle(p.getX(),p.getY(),10,32); 
      ball = new Rectangle(getX(),getY(),8,8); 

      if (player.intersects(ball)) 
      { 
       // A Collision! 
       // we know which enemy (e), so we can call e.DoCollision(); 
       b.score += 1; 


       if(cx == -1) { 
        cx = 1; 
       } else { 
        if(cx == 1) { 
         cx = -1; 
        } 
       } 
       System.out.println("Collided"); 
      } else { 
      } 
     } 


     public void move() { 
      System.out.print("MOVING"); 
      x += cx; 


     } 

     public void setX(int xs) { 
      x = xs; 
     } 

     public void setY(int ys) { 
      y = ys; 
     } 
     public Image getImage(){ 
      return img; 
     } 

     public int getX(){ 
      return x; 
     } 

     public int getY() { 
      return y; 
     } 


} 

錯誤是發生在這個peice的代碼代碼:

player = new Rectangle(p.getX(),p.getY(),10,32); 

在先進的感謝。

回答

5

您的Ball類具有p類型的字段,其類型爲Player

但是你從來沒有賦值到該字段,所以它永遠是null

試圖打電話給那個null值的任何方法肯定會導致NullPointerException

要麼需要確保每個BallPlayer分配(最好在施工期間)改變你的代碼來處理缺乏Player的(即檢查null在適當情況下)。

此外,Rectangle領域playerball似乎只在checkCollision方法可以使用,因此應在該方法中聲明局部變量和領域。

0

你確定你爲Ball的'p'(Player)設置了值嗎? 'p'未初始化。

0

變量p尚未初始化。它應該在哪裏?