Hey guy's im 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;
}
}
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 occuring at this peice of code:
player = new Rectangle(p.getX(),p.getY(),10,32);
Thanks in advanced.
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
(ie 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.