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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:42:41+00:00 2026-05-25T23:42:41+00:00

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

  • 0

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;
                   }
               }
               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);
  • 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-05-25T23:42:42+00:00Added an answer on May 25, 2026 at 11:42 pm

    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.

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

Sidebar

Related Questions

I'm making a game in Java. Every enemy in the game is a thread
I am making a game in Java using the framework pulpcore, I keep getting
I am making a game in JAVA where I want to come up with
I am making a text based game in Java. I have a text field,
I'm making a Java application with an application-logic-thread and a database-access-thread. Both of them
I am making a 2D game in Android with Cocos2D, written in Java. Here
I'm making a 2D game in Java and one of the main issues causing
I'm making a game in Java, and I need a good file format to
I'm making a torpedo game for school in java with swing gui, please see
I'm currently making a pacman game in java. I have a question about the

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.