An Error with simple KeyListener app
<p>Basiclly i am a begginer proggrammer, i know little bit from C#, C++
and now i am learning Java for building Android apps.
i am learning java from youtube from brandonio production, very helpful. <a href="http://www.youtube.com/user/BrandonioProductions?feature=watch" rel="nofollow">http://www.youtube.com/user/BrandonioProductions?feature=watch</a></p>

<p>this is the full code:</p>

<pre><code>import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class KeyListenerApp 
extends Applet
implements KeyListener{
private Rectangle rect; //The rectangle that moving
private ArrayList&lt;Integer&gt; keysDown;
public void init()
{
    rect = new Rectangle(0,0,50,50);
    this.addKeyListener(this);
}
public void paint(Graphics g)
{
    setSize(500,500);
    Graphics2D g2 = (Graphics2D)g;
    g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
    if(keysDown.contains(new Integer(e.getKeyCode())))
    {
        keysDown.add(new Integer(e.getKeyCode()));
    }
    moveRect();
}
@Override
public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect()
{
    int x = rect.x;
    int y = rect.y;
    if (keysDown.contains(KeyEvent.VK_UP))
        y -= 2;
    if (keysDown.contains(KeyEvent.VK_DOWN))
        y += 2;
    if (keysDown.contains(KeyEvent.VK_LEFT))
        x -= 2;
    if (keysDown.contains(KeyEvent.VK_RIGHT))
        x += 2;
    rect.setLocation(x, y);
    repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}
</code></pre>

<p>This is the full error:</p>

<pre><code>Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at KeyListenerApp.keyPressed(KeyListenerApp.java:34)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at KeyListenerApp.keyReleased(KeyListenerApp.java:44)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(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$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)
</code></pre>

<p>Sorry for not posting the code in here at first, i tought that it will be more comfortable for you if it will be seperate from the question.</p>

<p>Thank you</p>

<p>Here is the working code:</p>

<pre><code>import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;


public class KeyListenerApp 
extends Applet
implements KeyListener{

private Rectangle rect; //The rectangle that moving

private ArrayList&lt;Integer&gt; keysDown;

public void init()
{
    rect = new Rectangle(0,0,50,50);
    this.addKeyListener(this);
    keysDown = new ArrayList&lt;Integer&gt;();
}

public void paint(Graphics g)
{
    setSize(500,500);
    Graphics2D g2 = (Graphics2D)g;
    g2.fill(rect);
}

@Override
public void keyPressed(KeyEvent e) {

    if(keysDown.contains(new Integer(e.getKeyCode())) == false)
    {
        keysDown.add(new Integer(e.getKeyCode()));
    }

    moveRect();
}

@Override
public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));

}

public void moveRect()
{
    int x = rect.x;
    int y = rect.y;

    if (keysDown.contains(KeyEvent.VK_UP))
        y -= 2;

    if (keysDown.contains(KeyEvent.VK_DOWN))
        y += 2;

    if (keysDown.contains(KeyEvent.VK_LEFT))
        x -= 2;

    if (keysDown.contains(KeyEvent.VK_RIGHT))
        x += 2;

    rect.setLocation(x, y);
    repaint();
}

@Override
public void keyTyped(KeyEvent e) {


}

}
</code></pre>

<p>what cause the error:</p>

<pre><code>if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
    keysDown.add(new Integer(e.getKeyCode()));
}
</code></pre>

<p>This code was like this before i fixed it:</p>

<pre><code>if(keysDown.contains(new Integer(e.getKeyCode())))
{
    keysDown.add(new Integer(e.getKeyCode()));
}
</code></pre>

<p>The Error happend because I said in the code:
if something exsits then add him, and it was suppose to add him only if he wasn't exsits.</p>
