Thread: Error with Keyboard movement

Results 1 to 3 of 3
  1. #1 Error with Keyboard movement 
    Duration Developer
    Intrum's Avatar
    Join Date
    Jul 2012
    Posts
    884
    Thanks given
    46
    Thanks received
    147
    Rep Power
    66
    Error with my keyboard listener's movement.

    When I try moving with the "UP" key I get this error...


    Spoiler for Error:

    Up pressed.
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.intrum.client.keyboard.KeyboardListener.keyPre ssed(KeyboardListener.java:25)
    at java.awt.Component.processKeyEvent(Component.java: 6225)
    at java.awt.Component.processEvent(Component.java:604 4)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Window.processEvent(Window.java:1836)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1850)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:712)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:990)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:855)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:676)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4502)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)




    Here's the classes that I made...


    Spoiler for Client.java:
    package com.intrum.client;

    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JFrame;
    import com.intrum.client.keyboard.KeyboardListener;
    import com.intrum.engine.entities.Player;

    public class Client extends JFrame {

    private static final long serialVersionUID = 1L;
    public static int currentVersion = 1;

    private Player p;

    public Client() {
    p = new Player(600/2, 500/2);
    setFocusable(true);
    setTitle("Intrum's Java Game");
    setSize(600, 500);
    setVisible(true);
    setResizable(false);
    addKeyListener(new KeyboardListener(p, this));
    setFocusTraversalKeysEnabled(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(3);
    }

    /**
    * The paint component -
    * used for drawing the
    * character and the map
    * @param g
    */
    public void paint(Graphics g) {
    super.paint(g);
    requestFocus();
    paintCharacter(g);
    }

    public void paintCharacter(Graphics g) {
    p.move();
    g.drawImage(p.getImage(), p.getX(), p.getY(), null);
    }

    }


    Spoiler for Entity.java:
    package com.intrum.engine.entities;

    public abstract class Entity {

    public int x = 0, y = 0;

    /**
    * Constructor
    * @param x
    * @param y
    */
    public Entity(int x, int y) {
    this.x = x;
    this.y = y;
    }

    /**
    * Handle's the entities tick & movement
    */
    public abstract void move();

    /**
    * Render's the entity
    */
    public abstract void render();

    /**
    * Get Entities X
    * @return
    */
    public int getX() {
    return x;
    }

    /**
    * Get Entities Y
    * @return
    */
    public int getY() {
    return y;
    }

    }


    Spoiler for KeyboardListener.java:
    package com.intrum.client.keyboard;

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import com.intrum.client.Client;
    import com.intrum.engine.entities.Player;

    public class KeyboardListener implements KeyListener {

    private Client c;

    private Player p;

    public KeyboardListener(Player player, Client c) {
    this.p = p;
    }

    @Override
    public void keyPressed(KeyEvent e) {
    switch(e.getKeyCode()) {

    case KeyEvent.VK_UP:
    System.out.println("Up pressed.");
    p.deltaX += p.speed;
    break;

    case KeyEvent.VK_DOWN:
    System.out.println("Down pressed.");

    break;

    case KeyEvent.VK_LEFT:
    System.out.println("Left pressed.");

    break;

    case KeyEvent.VK_RIGHT:
    System.out.println("Right pressed.");

    break;

    }

    }

    public void refresh() {
    c.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    }


    Spoiler for Player.java:
    package com.intrum.engine.entities;

    import java.awt.Image;

    import javax.swing.ImageIcon;

    public class Player extends Entity {

    public int
    speed = 1,
    deltaX = 0,
    deltaY = 0;

    public Image
    still,
    left,
    right,
    backwards;

    public ImageIcon s = new ImageIcon("cache/sprites/stand.png");
    public ImageIcon l = new ImageIcon("cache/sprites/standLeft.png");
    public ImageIcon r = new ImageIcon("cache/sprites/standRight.png");
    public ImageIcon b = new ImageIcon("cache/sprites/standUp.png");

    /**
    * Player Constructor
    * @param x
    * @param y
    */
    public Player(int x, int y) {
    super(x, y);
    still = s.getImage();
    }

    @Override
    public void move() {
    x += deltaX;
    y += deltaY;
    }

    @Override
    public void render() {
    // TODO Auto-generated method stub

    }

    public void deltaX(int value) {
    deltaX = value;
    }

    public void deltaY(int value) {
    deltaY = value;
    }

    public Image getImage() {
    return still;
    }

    }




    - Thanks in advanced

    PLEASE NO HATE - JUST CONSTRUCTIVE RESPONSES/BETTER WAYS TO HANDLE THIS

    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Jun 2007
    Posts
    2,237
    Thanks given
    267
    Thanks received
    411
    Rep Power
    1283
    The 'Client c' in Keyboardlistener is never initalised
    Code:
    public KeyboardListener(Player player, Client c) {
    this.p = p;
    }
    add
    Code:
    this.c = c
    Don't worry, Be happy.
    Reply With Quote  
     

  3. #3  
    Duration Developer
    Intrum's Avatar
    Join Date
    Jul 2012
    Posts
    884
    Thanks given
    46
    Thanks received
    147
    Rep Power
    66
    Quote Originally Posted by Daniel View Post
    The 'Client c' in Keyboardlistener is never initalised
    Code:
    public KeyboardListener(Player player, Client c) {
    this.p = p;
    }
    add
    Code:
    this.c = c
    Next time try help me
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •