空指针异常?

时间:2011-10-06 07:21:01

标签: java nullpointerexception

嘿家伙我正在制作一个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;
        }


}

在这段代码中出现错误:

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

先谢谢。

4 个答案:

答案 0 :(得分:5)

您的Ball班级有一个名为p的字段Player

但您从未为该字段指定值,因此它始终为null

尝试调用该null值的任何方法肯定会产生NullPointerException

要么需要确保每个Ball分配Player(理想情况下在构建期间)更改代码以处理缺陷一个Player(即在适当情况下检查null)。

此外,Rectangle字段playerball似乎只在checkCollision方法中使用,因此应该是在该方法中声明的局部变量不是字段。

答案 1 :(得分:0)

你确定你为Ball的'p'(玩家)设定了价值吗? 'p'未初始化。

答案 2 :(得分:0)

变量p尚未初始化。它应该在哪里?

答案 3 :(得分:-1)

你有nullThis link它将向您解释java NullPointerException的含义。