-3

I am trying to get the timer working to move the ball. It just isn't working. I am getting alot of errors which i don't yet understand

Can someone tell me what i am doing wrong.

Here is the error that i got.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Paneel$TimerHandler.actionPerformed(Paneel.java:30)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)

Thanks in advance.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Paneel extends JPanel 
{
    private int height, width;
    private boolean moveLeft, moveRight, moveUp, moveDown;
    private Timer timer;
    private Ball ball;

    public Paneel() 
    {
        TimerHandler timerHandler = new TimerHandler();
        timer = new Timer(20, timerHandler);
        timer.start();
    }

    public void paintComponent(Graphics pen)
    {
        super.paintComponent(pen);
        ball = new Ball((double)getWidth(), (double)getHeight());
        ball.drawBall(pen);
    }

    class TimerHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            ball.moveDown();
            repaint();
        }
    }
}
IvoryNL
  • 41
  • 1
  • 9

1 Answers1

0

You are creating a new Ball object during each paint

ball = new Ball((double)getWidth(), (double)getHeight());

That means that the ball reference is null up until the first paint call.

ball.moveDown(); // ball here could be null

I recommend defining ball in the constructor for now. I'll need to see the source of the Ball class before I can figure out how to set its size correctly.

public Paneel() 
{
    ball = new Ball(0, 0);
    TimerHandler timerHandler = new TimerHandler();
    timer = new Timer(20, timerHandler);
    timer.start();
}
Cliabhach
  • 1,402
  • 2
  • 12
  • 19
  • `That means that the ball reference is null up until the first paint call` How could that be if OP already instantiated the ball object before method invocation? – user3437460 Dec 15 '17 at 22:06
  • It would not happen then. However, that is not what the code in the question does. There, if a caller created a new `Paneel()` then waited 20 milliseconds, the `TimerHandler` created in the constructor will have its `actionPerformed` called while `ball` was still null. – Cliabhach Dec 15 '17 at 22:12
  • I am having trouble gettting the height and width the panel. It is 0 when i use it in the constructor. Do you have a solution for that? – IvoryNL Dec 15 '17 at 22:13
  • @IvoryNL I think you can use a component listener from wherever you create the `Paneel`. There's some info on that over at https://stackoverflow.com/a/1088664/3934789 . – Cliabhach Dec 15 '17 at 22:21