当我从另一个类调用我的refresh()方法时出现错误。
public void refresh() {
repaint();
}
JFrame
是refresh()
,而我是从普通班级调用的。错误是空指针,它位于我的level.refresh()
和public void refresh()
上。
我想要做的就是完全正常,但我收到了这个错误
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;
}
}
等级:
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);
}
}
}
`
答案 0 :(得分:1)
如所怀疑的那样,您的levelp
变量指向任何内容(null
)。
当你看到这一行时,你会发现你试图在一个实际上没有引用任何内容的变量上调用refresh()
方法:
levelp.refresh();
刚刚声明为
public static Level levelp;
没有任何实际创建Level
对象的代码(ergo:你试图在指向null
的引用上调用方法。)
尝试创建一个这样的直接对象:
public static Level levelp = new Level();
我还建议您仔细阅读this post,以便了解NullPointerException
的发生方式和时间!