-2

I'm getting an error when I call my refresh() method from another class.

public void refresh() {
    repaint();
}

It's a JFrame that the refresh() is in and I'm calling it from a normal class. The error is a Null Pointer, and it is on my level.refresh(), as well as as public void refresh(). What I am trying to do is working completely fine, but I'm getting this error

Player class`package entity.player;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

import level.Level;

public class Player {
public static Image playerImgLeft = new ImageIcon(
        "resources/Player/left/player_anim1_left.png").getImage();
public static Image playerImgRight = new ImageIcon(
        "resources/Player/right/player_anim1_right.png").getImage();
public static Level levelp;
public int x = 0;
public int y = 0;
public int dy = 0;
public int dx = 0;
// Direction = right
public int direction = 0;

public Player() {
    x = 10;
    y = 10;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public Image getImageLeft() {
    return playerImgLeft;
}

public Image getImageRight() {
    return playerImgRight;
}

public void move() {
    x = x + dx;
    y = y + dy;

}

public void keyPressed(KeyEvent key) {
    if (key.getKeyCode() == KeyEvent.VK_A) {
        dx = -1;
        direction = 1;
        System.out.println("left");

        levelp.refresh();

    }
    if (key.getKeyCode() == KeyEvent.VK_D)
        dx = 1;
    direction = 0;

    if (key.getKeyCode() == KeyEvent.VK_W)
        dy = -1;

    if (key.getKeyCode() == KeyEvent.VK_S)
        dy = 1;

}

public void keyReleased(KeyEvent key) {

    if (key.getKeyCode() == KeyEvent.VK_A)
        dx = 0;

    if (key.getKeyCode() == KeyEvent.VK_D)
        dx = 0;

    if (key.getKeyCode() == KeyEvent.VK_W)
        dy = 0;

    if (key.getKeyCode() == KeyEvent.VK_S)
        dy = 0;

}

}

Level class:

package level;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

import entity.player.Player;

public class Level extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;
public static int level = 0;
Player player;
public Timer time;
public Image img;

public Level() {
    this.setVisible(true);
    setFocusable(true);
    player = new Player();
    this.addKeyListener(new LevelKeyListener());
     ImageIcon i = new ImageIcon("");
     img = i.getImage();

    time = new Timer(5, this);
    time.start();
}

public void actionPerformed(ActionEvent e) {
    player.move();

    repaint();
    revalidate();
}

public void refresh () {
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);


    Graphics2D g2d = (Graphics2D) g;

     g.drawImage(img, 0, 0, null);
     if(player.direction == 0) {
    g2d.drawImage(player.getImageRight(), player.getX(), player.getY(), null);
     } else if(player.direction == 1) {
         g2d.drawImage(player.getImageLeft(), player.getX(), player.getY(),    null);

     }

}

private class LevelKeyListener extends KeyAdapter {

    public void keyPressed(KeyEvent e) {
        player.keyPressed(e);
    }

    public void keyReleased(KeyEvent e) {
        player.keyReleased(e);
    }
}
}

`

loafy.
  • 103
  • 10

1 Answers1

1

As was suspected, your levelp variable points to nothing (is null).

When you look at this line you'll see that you're trying to call the refresh() method on a variable that isn't actually referencing anything:

levelp.refresh();

which is just declared as

public static Level levelp;

without any code that actually creates a Level object (ergo: you are trying to call a method on a reference that points to null).

Try creating an immediate object like this:

public static Level levelp = new Level();

I also encourage you to read through this post so you understand how and when NullPointerExceptions occur!

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • * Epic fail* I thought when I called the new Level() it would create a new JPanel. *Fail* – loafy. Feb 10 '14 at 23:20
  • I beg to differ. The levelp field should most definitely **not** be a static field. @user3234092: you should make this change, change it into an *instance* field. – Hovercraft Full Of Eels Feb 10 '14 at 23:39