1

I am making a small hobby game, I have a couple of classes so far. There are two classes that I am having some issues with, the classes in question are listed below.

Screen.Java

package geisst.flat;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Screen extends JFrame {

public int x = 100;
public int y = 100;

public Screen() {
    this.setSize(400, 400);
    this.setTitle("Flat Game");
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    addKeyListener(new KeyListen());
}

public void paint(Graphics g) {
    BufferedImage mainImage = null;

    try {
        mainImage = ImageIO.read(new File("res/test.gif"));
    } catch(IOException e) {

    }

    g.drawImage(mainImage, x, y, null);
    repaint();
}
}

And KeyListen.Java

package geisst.flat;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListen extends KeyAdapter {

Screen screen;

@Override
public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if(keyCode == KeyEvent.VK_LEFT) {
        screen.x += 3;
    }
}

}

KeyListen is supposed to move the mainImage's x position up by 3 pixels, but I recieve the following error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at geisst.flat.KeyListen.keyPressed(KeyListen.java:15)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.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.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)

Do you guy's and gal's have any idea why this would be happening? It's probably something really obvious :P

Thanks in advance,

GeissT

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Todd Hainsworth
  • 114
  • 1
  • 7

3 Answers3

2

You are never initializing the value of screen.

What you can do is something like this:

In the KeyListener Class

public KeyListen (Screen screen) {
 this.screen = screen;
}

and in the Screen Class:

addKeyListener(new KeyListen(this));
npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thank you very much, this worked perfectly, sorry if this was so obvious that I seemed like a complete idiot, I am still learning about this topic. – Todd Hainsworth Dec 19 '11 at 12:04
1

You never initialize screen, so it is null, and yet you use it's properties:

screen.x += 3;

You should pass a reference to the Screen object to the listener, or by calling getSource:

@Override
public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if(keyCode == KeyEvent.VK_LEFT) {
        if (e.getSource() instanceof Screen)
            ((Screen)e.getSource()).x += 3;
    }
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • If I initialize the screen will that make a new instance of the windows on my screen? Sorry if my question came of a little unintelligent. – Todd Hainsworth Dec 19 '11 at 11:56
  • Thank you also, this worked great, I chose npinti's solution as it looked simpler but your's is also a great solution. – Todd Hainsworth Dec 19 '11 at 12:05
1

1) not good idea paint directly to the JFrame, put there JPanel, JComponent or JLabel

2) for Swing JComponents is there paintComponent instead of paint, paint is correct method for painting to the RootPane or GlassPane (derived Component from JFrame)

3) use KeyBindings rather than KeyListener, because KeyListener doesn't works without Focus in the Window, not good idea to setFocusable to the ContentPane

4) use JComponent for Custom Painting or JLabel with Icon

5) example for KeyBindings here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319