views:

15

answers:

2

I keep getting the same error when I try to run my program and press a key.

The error indicates the problem is in the keyTyped function, but it looks good to me.

What do you think?

Code:

//Key Listener.
private class GameKeyListener implements KeyListener {
 public void keyTyped (KeyEvent event) {
  char inputKey = 'd';
  if(isStart == true){
   try{
    inputKey = event.getKeyChar();
   } catch(Exception e){
    System.out.println("Bad input.");
   }
   if(inputKey == 's'){
    isStart = false;
    timer.start();     // Add timer. Start game here.
   }
  }  
 }
 public void keyPressed (KeyEvent event) {};
 public void keyReleased (KeyEvent event){};
}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at RoofRunnerGame$GameKeyListener.keyTyped(RoofRunnerGame.java:106)
at java.awt.Component.processKeyEvent(Component.java:6303)
at java.awt.Component.processEvent(Component.java:6125)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Window.processEvent(Window.java:1836)
at java.awt.Component.dispatchEventImpl(Component.java:4714)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4544)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
at java.awt.Component.dispatchEventImpl(Component.java:4586)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4544)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
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)
+1  A: 

I think a variable isn't initialized correctly at line 106. Maybe timer?

Vivien Barousse
A: 

In the line

timer.start();     // Add timer. Start game here

are you sure timer is not null?

Nivas