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 8215903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:56:37+00:00 2026-06-07T11:56:37+00:00

Ball.java public class Ball { int x = 500, y = 10, speed =

  • 0

Ball.java

public class Ball {
    int x = 500, y = 10, speed = 1;
    GameBoard board;
    boolean keepRunning = true;
    Thread thread;

    Ball(GameBoard board) {
        this.board = board;
        new Thread(r1).start();
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    Runnable r1 = new Runnable() {
        public void run() {
            try {
                while (true) {
                    if (board.ball.intersects(board.pPaddle)) {
                        speed = -speed;
                    } else {
                        System.out.println(board.ball + " z " + board.pPaddle);
                    }
                    x -= speed;
                    board.repaint();
                    Thread.sleep(10L);

                }
            } catch (InterruptedException iex) {
            }
        }

    };
}

GameBoard.java

@SuppressWarnings("serial")
public class GameBoard extends Canvas {
    Image dbi;
    Graphics db;
    JFrame okno;
    Player p = new Player(this);
     Ball b = new Ball(this);
     Ai a = new Ai(this);
     Rectangle aiPaddle = new Rectangle(10, 590, 10, 50);
     Rectangle pPaddle = new Rectangle(10, 100, 10, 50);
     Rectangle ball = new Rectangle(500, 10, 10, 10);
    GameBoard() {
        okno = new JFrame();
        okno.setTitle("Pink Ponk");
        okno.setSize(600, 300);
        okno.getContentPane().setBackground(Color.black);
        okno.setResizable(false);
        okno.setVisible(true);
        okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(p);
    }

    public static void main(String[] args) {
        GameBoard gra = new GameBoard();
        gra.okno.add(gra);
    }

    @Override
    public void update(Graphics g) {
        pPaddle.setBounds(p.getX(), p.getY(), 10, 50);
        aiPaddle.setBounds(a.getX(), a.getY(), 10, 50);
        ball.setBounds(b.getX(), b.getY(), 10, 10);
        dbi = createImage(10, 50);
        db = dbi.getGraphics();
        paint(db);
        g.clearRect(0, 0, 600, 300);
        g.drawRect(p.getX(), p.getY(), 10, 50);
        g.fillRect(p.getX(), p.getY(), 10, 50);
        g.drawOval(b.getX(), b.getY(), 10, 10);
        g.fillOval(b.getX(), b.getY(), 10, 10);
    }

    @Override
    public void paint(Graphics g) {
        pPaddle.setBounds(p.getX(), p.getY(), 10, 50);
        aiPaddle.setBounds(a.getX(), a.getY(), 10, 50);
        ball.setBounds(b.getX(), b.getY(), 10, 10);
        g.clearRect(0, 0, 600, 300);
        g.setColor(Color.white);
        g.drawRect(p.getX(), p.getY(), 10, 50);
        g.fillRect(p.getX(), p.getY(), 10, 50);
        g.drawOval(b.getX(), b.getY(), 10, 10);
        g.fillOval(b.getX(), b.getY(), 10, 10);
    }

}

It worked until I wrote this line of code (24 in Ball.java):

if (board.ball.intersects(board.pPaddle))

I’m getting error:

Exception in thread “Thread-0” java.lang.NullPointerException at
Ball$1.run(Ball.java:24) at java.lang.Thread.run(Unknown Source)

I’m sure that rectangles in GameBoard.java aren’t empty. I don’t know what to do.

  • 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-07T11:56:39+00:00Added an answer on June 7, 2026 at 11:56 am

    As far as I can see, there are 2 possibilities:

    • board, the argument passed to the Ball’s constructor, is null
    • board, the argument passed to the Ball’s constructor, is not null but because you start a thread in the constructor, the thread can see your Ball object partially constructed (and for example, ball.board might have not been assigned yet)…

    What you sould do:

    1. Check that board is not null (with a print statement for example)
    2. In any case, don’t start a new thread in your constructor

    You can create the thread in the consctructor and provide another method (start or init for example) that starts the thread.


    Once you have done that, you will probably not get your NPE any longer. However, note that the line Ball b = new Ball(this); is executed before Rectangle ball = new Rectangle(500, 10, 10, 10); so if you access board.ball in the Ball’s constructor, you will see it null.

    In general, you should avoid escaping this in the constructor or an instance initializer (i.e. don’t write something like ObjectXX o = new ObjectXX(this);) because this might not be fully constructed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.*; public class PossibilityGame { private List<String> list1, list2, list3, list4; public PossibilityGame()
I am writing a bouncing ball class (I'm just starting to use Java) and
I have got this exercise working perfectly: import acm.program.*; import acm.graphics.*; import java.awt.event.*; public
I am developing a game in java just for fun. It is a ball
I am new to Silverlight and want to animate a ball that is shot
I have got a ball which bounces of walls. This bounce is simple, i
I have a class called Ball, and i want to call a method called
The problem is that this ball after it is dragged and exited click, it
This is my code.I am trying to make the ball move using arrow keys.
I'm making a program in Java for my CS class. My teacher has little

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.