Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:23:47+00:00 2026-06-14T09:23:47+00:00

Ok, I’m semi new to java and I am making a pong game. I

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:23:48+00:00Added an answer on June 14, 2026 at 9:23 am
    public Ball ball;  // ball is not initialized
    public int BallX = ball.BallLocationX;  // NPE here
    public int BallY = ball.BallLocationY;
    

    Here’s the problem. In your instance variable declaration, your ball is still pointing to null and you have used it to access BallLocationX. It will throw a NPE.

    You should initialize your ball reference to point to an instance of Ball first: –

    public Ball ball = new Ball();  // Or whatever way you use to instantiate it
    public int BallX = ball.BallLocationX;
    public int BallY = ball.BallLocationY;
    

    An Advice : –

    • I just noticed that, you have used public modifier for all your
      fields. You should not do that. As far as possible, try to have
      private modifier for your fields, and provide public accessors to
      access them. (getters and setters).
    • Also, follow Java Naming Conventions. Variable names should start with lowercase alphabets/underscore/dollar symbol (Change BallX to ballX and BallLocationX to ballLocationX)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.