开发者

Nullpointer Exception?

开发者 https://www.devze.com 2023-04-10 21:02 出处:网络
I am making a java game and I am getting the following error: actionPerformedException in thread "AWT-EventQueue-0" java.lang.NullPointerException

I am making a java game and I am getting the following error:

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)

It is happening when my game checks for collision between the ball and my other object. This is my code for 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;
    开发者_StackOverflow中文版               }
               }
               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;
        }
    

}

The error is occurring at this piece of code:

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


Your Ball class has a field called p that is of type Player.

But you never assign a value to that field, so it will always be null.

Trying to call any method on that null value will surely result in a NullPointerException.

You either need to ensure that every Ball has a Player assigned (ideally during construction) or change your code to handle the lack of a Player (i.e. check for null where appropriate).

Also, the Rectangle fields player and ball only seem to be used in the checkCollision method and therefore should be local variables declared in that method and not fields.


Are you sure you set value for 'p' (Player) of Ball? 'p' is not initialized.


The variable p hasn't been initialized. Where is it supposed to be?


You have a null. This link it will explain to you what the meaning of a java NullPointerException.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号