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

  • Home
  • SEARCH
  • 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 8040549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:56:00+00:00 2026-06-05T03:56:00+00:00

I have been searching for an answer to this for two days now, but

  • 0

I have been searching for an answer to this for two days now, but that didn’t seem to solve much. Mostly because i don’t really understand what is causing my error in the first place, nor would i know how to go about fixing the problem.

I’ve been trying to make an “animation” of sorts, where one of the layers in it spawn circles on the right side of the screen and send them to the left over time. Problem is every time it is meant to spawn a circle, it doesn’t appear and i get an error.

Exception in thread "Thread-4" java.lang.NullPointerException
at Ball.draw(Ball.java:39)
at Ball.run(Ball.java:23)

Here is the class that is causing the errors:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;


public class Ball extends Thread{
    JPanel drawingPanel;
    private int x=400,y=200;
    private int dx=-2,dy=0;
    private static final int XSIZE=10,YSIZE=10;
    Random rdm = new Random();
    int y_rdm = rdm.nextInt(440)+30;

    public Ball(JPanel jp){
        drawingPanel=jp;
        dx-=1;

    }
    public void run(){
        draw();
        for (int i=0;i<1000;i++){
            try{
                Thread.sleep(10);
            }
            catch(InterruptedException e){} 
            move();
        }
    }
    private void move(){
        erase();
        changePos();
        draw();
    }
    private void draw(){
        Graphics g = drawingPanel.getGraphics();
        g.setColor(Color.WHITE);
        g.fillOval(x,y_rdm,XSIZE,YSIZE);
        g.dispose();
    }
    private void erase(){
        Graphics g=drawingPanel.getGraphics();
        g.setColor(drawingPanel.getBackground());
        g.fillOval(x,y_rdm,XSIZE,YSIZE);
        g.dispose();
    }

    private void changePos(){
        x+=dx;y_rdm+=dy;
    }



}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class BallPanel extends JPanel implements ActionListener{

    JPanel drawingPanel = new JPanel();
    public BallPanel(){

      int delay = 300;
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              Ball b= new Ball(drawingPanel);
                b.start();
          }
      };

      new Timer(delay, taskPerformer).start();

    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }

}

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.InputStream;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javazoom.jl.player.Player;


public class Window extends JFrame{

      private JLayeredPane layerpane;
      private JPanel up, down;

    public Window(){
          layerpane = new JLayeredPane();

          down = new JPanel();
          down.setBounds(0, 0, 450, 450);
          down.setBackground(new Color(0, 51, 102));
          layerpane.add(down, new Integer(1));

          BallPanel bp = new BallPanel();
          layerpane.add(bp, new Integer(2));
          bp.setBounds(0, 0, 450, 450);
          bp.setOpaque(!bp.isOpaque());

          Animation2 ani2 = new Ani2();
          ani2.setBounds(0, 0, 400, 450);
          layerpane.add(ani2, new Integer(3));

          ani2.setOpaque(!ani2.isOpaque());

         getContentPane().add(layerpane, BorderLayout.CENTER);

    }

        public static void main(String args[]){

        JFrame f = new Window();

        f.setVisible(true);
        f.setSize(450,450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        try{
            URL url = new URL("file:///C://song.mp3");
            InputStream in = url.openStream();
            Player pl = new Player(in);
            pl.play();
            }        

            catch(Exception e){
                e.printStackTrace();
            } 


        }

    }

Please tell me if i need to include my other classes, they just seem slightly irrelevant.

  • 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-05T03:56:02+00:00Added an answer on June 5, 2026 at 3:56 am

    The getGraphics method of JComponent returns the Graphics object associated with the Component. But if there is no Graphics object associated it will return null, which is the case in your code.

    A Component is associated with a Graphics object only when it is added to a container that is associated with one, or if it is a Top-Level Container (JFrame, JDialog, and JApplet).

    Your problem here is that your JPanel is not contained inside any top-level container, so its associated Graphics object is null.

    To fix the problem, either make sure you have added the JPanel to a top-level container before you call getGraphics() on it, or instead extend JPanel and override its paintComponent(Graphics g) method to do the drawing (second option is preferred since the paintComponent method is called automatically whenever the parent container needs to be redrawn (e.g. if it was resized, or blocked and unblocked by some other window, …)).

    (By the way, in your BallPanel class which extends JPanel, you define a new JPanel and pass it to Ball‘s constructor. You should be passing this to the the constructor, since the drawingPanel is never added to any container, but the BallPanel instance itself is. It really doesn’t make sense to define a JPanel field inside a class which is itself a JPanel and have the field do the job that the class itself is supposed to do.)

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

Sidebar

Related Questions

I have been searching around for an answer to this but I can't seem
I have been searching for an answer to my problem for two days now,
this question might have been answered before, but after two days of searching I
For the last two days I have been searching for an answer to this
I have been searching and searching for this answer. But I cannot seem to
I have been searching for an answer and trying out stuff for days now
I have been searching for an answer for a couple days now and had
I have been searching desperately for an answer to this question, but not found
I've been searching for this for a few days now, but nothing seems to
I have been searching for an answer to this question but cannot find anything

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.