Ok, I’m semi new to java and I am making a pong game. I want to do it completely by myself, but I’ve come to a problem. I have got 2 classes so far. My main one, and one which contains information about the ball.
My main class is as follows:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Main extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static int Width=800;
public static int Height=600;
public boolean Running=false;
public Thread thread;
public Ball ball;
public int BallX = ball.BallLocationX;
public int BallY = ball.BallLocationY;
public static void main(String[] args){
Main game = new Main();
JFrame frame = new JFrame();
frame.setSize(Width, Height);
frame.setTitle("Pong By Poo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.add(game);
game.start();
}
public void start(){
if(Running==true){
return;
}
else {
Running=true;
thread = new Thread(this);
thread.start();
}
}
public void run(){
while(Running==true){
Draw();
}
}
public void Draw(){
BufferStrategy bs = this.getBufferStrategy();
if(bs==null){
createBufferStrategy(2);
}else{
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillOval(BallX, BallY, 10, 10);
}
}
}
And my ball class is this:
**import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Ball extends JPanel{
public int BallLocationX;
public int BallLocationY;
public boolean BallMovementY; //true makes the ball go up, false makes it go down
public boolean BallMovementX; //True makes the ball go right, false makes it go left.
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(BallLocationX, BallLocationY, 10, 10);
}
//moves the ball left to right
public int YVelocity(){
if(BallMovementY==true){
BallLocationY++;
}
else{
BallLocationY--;
}
return BallLocationY;
}
//Moves the ball up and down
public int XVelocity(){
if(BallMovementX==true){
BallLocationX++;
}
else{
BallLocationX--;
}
return BallLocationX;
}
}
**
Im trying to draw the ball on the screen inside my main class, useing the location of the ball which I get from the ball class.
I know that (As of yet) The ball wont move, Ill figure that out later. My probmlem is that it wont draw the ball on screen, getting me this error:
Exception in thread "main" java.lang.NullPointerException
at Main.<init>(Main.java:20)
at Main.main(Main.java:26)
Thanks!
Here’s the problem. In your instance variable declaration, your
ballis still pointing tonulland you have used it to accessBallLocationX. It will throw aNPE.You should initialize your
ballreference to point to an instance ofBallfirst: –An Advice : –
public modifierfor all yourfields. You should not do that. As far as possible, try to haveprivate modifierfor your fields, and providepublic accessorstoaccess them. (getters and setters).
BallXtoballXandBallLocationXtoballLocationX)