# Hilfe bei Pong mit Bluej



## Werners1 (12. Jan 2009)

Hallo
Wir haben ein Problem mit unserem Schulprojekt Pong.
Wenn wir den Start-button drücken, kommt nur folgende Fehlermeldung:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at PongCanvas.keyTyped(PongCanvas.java:144)
	at java.awt.Component.processKeyEvent(Component.java:5443)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2713)
	at java.awt.Component.processEvent(Component.java:5265)
	at java.awt.Container.processEvent(Container.java:1966)
	at java.awt.Component.dispatchEventImpl(Component.java:3955)
	at java.awt.Container.dispatchEventImpl(Container.java:2024)
	at java.awt.Component.dispatchEvent(Component.java:3803)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636)
	at java.awt.Component.dispatchEventImpl(Component.java:3841)
	at java.awt.Container.dispatchEventImpl(Container.java:2024)
	at java.awt.Window.dispatchEventImpl(Window.java:1774)
	at java.awt.Component.dispatchEvent(Component.java:3803)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)"

Kann uns vielleicht damit jemand weiterhelfen?
Unser Projekt ist in mehrere Teile aufgeteilt. Den Quelltext der (wahrscheinlich) fehlerhaften Datei:

```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class PongCanvas extends JPanel implements KeyListener {
 
    private int width;
    private int height;

    private PongBall ball;
    private PongPaddle paddle1, paddle2;
    private PongBallTimer TimerX;
    private java.util.Timer ballTimer;
   

    public PongCanvas (int width, int height) {
        this.width = width;
        this.height = height;

        ball = new PongBall(width/2, height/2, 3, 4);

        paddle1 = new PongPaddle((height/2)-26, 5, 10, 70);
        
        paddle2 = new PongPaddle((height/2)-26, width-5, 10, 70);
        TimerX = new PongBallTimer(ball,this,paddle1,paddle2);
        this.addKeyListener(this);
    }
    
    public boolean isFocusable() {
        return true;
    }

   
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }

    public Dimension getMinimumSize() {
        return new Dimension(width, height);
    }

    public Dimension getMaximumSize() {
        return new Dimension(width, height);
    }
   
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillOval(ball.getHorizPos()-4,ball.getVertPos()-4,8,8);
        
        g.setColor(Color.blue);
        
        g.fillRect(paddle1.getLeftEdgePos()+5,
                   paddle1.getTopEdgePos(),
                   paddle1.getRightEdgePos(),
                   paddle1.getBottomEdgePos());
        g.setColor(Color.green.darker());
        g.fillRect(paddle2.getLeftEdgePos()-14,
                   paddle2.getTopEdgePos(),
                   paddle2.getRightEdgePos(),
                   paddle2.getBottomEdgePos());
    }
    
    
 public void paintBorder(Graphics g) {
        g.setColor(Color.black);
        g.drawRect(5,7,width-10,height-14);
    }
public void keyTyped(KeyEvent e) {
         
    
       
   
        if (e.getKeyChar() == 'a' ||
            e.getKeyChar() == 'A') {
            if (paddle1.y > 7) {
                paddle1.y = paddle1.y - 9;
                repaint();
            }
        }
        else if (e.getKeyChar() == 'y' ||
                 e.getKeyChar() == 'Y') {
            if (paddle1.y < (height - 77)) {
                paddle1.y = paddle1.y + 9;
                repaint();
            }
        }
        else if (e.getKeyChar() == 'k' ||
                 e.getKeyChar() == 'K') {
            if (paddle2.y > 7) {
                paddle2.y = paddle2.y - 9;
                repaint();
            }
        }
        else if (e.getKeyChar() == 'm' ||
                 e.getKeyChar() == 'M') {
            if (paddle2.y < (height - 77)) {
               paddle2.y = paddle2.y + 9;
                repaint();
            }
        }             
        else if (e.getKeyChar() == 'g' || e.getKeyChar() == 'G') {
            ballTimer.scheduleAtFixedRate(TimerX,0,40);
        
    }
        } }
```


----------



## SlaterB (12. Jan 2009)

ballTimer ist immer null,
der Zugriff
>  ballTimer.scheduleAtFixedRate(TimerX,0,40); 
bringt die Exception


----------



## Werners1 (12. Jan 2009)

Danke! aber wie können wir dann den fehler beheben?
Wir brauchen den BallTimer ja für unser Spiel


----------



## SlaterB (12. Jan 2009)

was ist denn mit dem
> PongBallTimer TimerX
hat der nix zu sagen?

ansonsten gilt nur: das Objekt darf nicht null sein, muss wahrscheinlich irgendwo gestetzt werden,
dieser Hinweis passt für quasi 99,9% aller Programme


----------



## Landei (12. Jan 2009)

```
private java.util.Timer ballTimer = new java.util.Timer();
```


----------

