0

The following program is simple and is supposed to draw a ball that falls down to the effect of gravity and then bounces back up. The program seems to compile but I keep getting an exception error.

In the class ball, the fonction move contains the physics formula to calculate the velocity and displacement.

package gravity;

import java.awt.Color;
import java.awt.Graphics;

public class Ball extends StartingPoint{
/**
     * 
     */
    private static final long serialVersionUID = 1L;
public int x;
public int y;

private static final double dt = .2;
private static final double g = 9.8;
public double vy = 0;

Ball(int x, int y){
    this.x = x;
    this.y = y;
}

public void move(){
    vy += g*dt;
    y += (1/2)*g * dt*dt + vy * dt;
    if (y >= (this.getHeight() - 20 -1)){
    y = this.getHeight() - 20 -1;
    vy = -vy;}

    }


public void paintBALL(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y, 20, 20);
}
}

package gravity;

import java.applet.Applet;
import java.awt.Graphics;

public class StartingPoint extends Applet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public int x = 400, y = 25;
    Ball ball;

    //private static final int nbBalls = 1;

    @Override
    public void init() {
        setSize(800, 600);

    }

    @Override
    public void start() {
        Ball ball = new Ball(x,y);
        Thread thread = new Thread(new RunBall(ball));
        thread.start();

    }

    public class RunBall implements Runnable{
        private Ball ball;
        RunBall(Ball ball){
            this.ball = ball;
        }
        @Override
        public void run() {
            while(true){

                ball.move();
                try {
                    Thread.sleep(17);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                repaint();
            }

        }

    }

    @Override
    public void stop() {

    }

    @Override
    public void paint(Graphics g) {
        ball.paintBALL(g);
    }

}

the compiler keeps adding the following exeption :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gravity.StartingPoint.paint(StartingPoint.java:61)
at java.awt.Container.update(Container.java:1835)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:267)
at sun.awt.RepaintArea.paint(RepaintArea.java:233)
at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
at java.awt.Component.dispatchEventImpl(Component.java:4852)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Philippe
  • 700
  • 1
  • 7
  • 17
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – azurefrog Oct 11 '14 at 20:05

2 Answers2

0

In your start method, you've re declared Ball...

    Ball ball = new Ball(x,y);

This means that the instance field ball is still null when paint is called.

Instead, in your start method, initialise the instance field...

ball = new Ball(x,y);

Applet is very out of date and you should be prepared for a lot of flickering as the applet is repainted.

If you can, I'd recommend using a JApplet but using a JPanel as the primary paint surface. By overriding the JPanels paintComponent method, you get double buffering support by default, which will stop the flickering without you doing any additional work, just don't forget to call super.paintComponent before y do any custom painting...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

paint is called before your Ball is defined, simply change your paint:

@Override
public void paint(Graphics g) {
  if(ball != null)    
    ball.paintBALL(g);
}
paulgavrikov
  • 1,883
  • 3
  • 29
  • 51