Java KeyEvent 出现 NullPointerException
Java NullPointerException on KeyEvent
我是 Java 的新手,这是我的第一个程序,除了一些 15 行打印类。每次我尝试编译 GameTutorial.java 时,第 57 行(在 update() 方法内)都会出现 NullPointerException。拜托,我需要帮助,因为我不知道自己做错了什么。可能是InputHandler.class,所以我也记下了。
游戏教程:
import input.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
public class GameTutorial extends JFrame{
InputHandler input;
public static void main(String[] args){
GameTutorial game = new GameTutorial();
game.run();
System.exit(0);
};
static int x=10;
static int windowHeight=600;
static int windowWidth=1200;
static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
public void run(){
boolean isRunning = true;
initialize();
while(isRunning){
long time = System.currentTimeMillis();
update();
draw();
time = (1000 / 30) - (System.currentTimeMillis() - time);
if (time > 0) {
try{
Thread.sleep(time);
}
catch(Exception e){};
};
};
setVisible(false);
};
public void initialize(){
setTitle("Game Tutorial");
setSize(windowWidth, windowHeight);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
};
public void update(){
if (input.isKeyDown(KeyEvent.VK_RIGHT)){
x += 5;
}
if (input.isKeyDown(KeyEvent.VK_LEFT)){
x -= 5;
}
};
public void draw(){
Color white = Color.WHITE;
Color black = Color.BLACK;
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.WHITE);
bbg.fillRect(0, 0, windowWidth, windowHeight);
bbg.setColor(Color.BLACK);
bbg.drawOval(x, 100, 20, 20);
g.drawImage(backBuffer, 0, 0, this);
};
}
输入处理程序:
package input;
import java.awt.Component;
import java.awt.event.*;
public class InputHandler implements KeyListener{
static boolean[] keys = new boolean[256];
public InputHandler(Component c){
c.addKeyListener(this);
}
public boolean isKeyDown(int keyCode){
if (keyCode > 0 && keyCode < 256){
keys[keyCode]=true;
return keys[keyCode];
}
return false;
}
public void keyPressed(KeyEvent e){
if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
keys[e.getKeyCode()] = true;
}
}
public void keyReleased(KeyEvent e){
if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
keys[e.getKeyCode()] = false;
}
}
public void keyTyped(KeyEvent e){}
}
您没有为 InputHandler 输入赋值。
input=new InputHandler(argument);
您的 input 字段在使用之前从未获得值。你必须在使用它的方法之前初始化它。
类似于:
input = new InputHandler(whatever);
希望对您有所帮助
编辑
根据您的代码,用这样的字段声明和初始化替换字段声明可能就足够了:
InputHandler input = new InputHandler(this);
我是 Java 的新手,这是我的第一个程序,除了一些 15 行打印类。每次我尝试编译 GameTutorial.java 时,第 57 行(在 update() 方法内)都会出现 NullPointerException。拜托,我需要帮助,因为我不知道自己做错了什么。可能是InputHandler.class,所以我也记下了。
游戏教程:
import input.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
public class GameTutorial extends JFrame{
InputHandler input;
public static void main(String[] args){
GameTutorial game = new GameTutorial();
game.run();
System.exit(0);
};
static int x=10;
static int windowHeight=600;
static int windowWidth=1200;
static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
public void run(){
boolean isRunning = true;
initialize();
while(isRunning){
long time = System.currentTimeMillis();
update();
draw();
time = (1000 / 30) - (System.currentTimeMillis() - time);
if (time > 0) {
try{
Thread.sleep(time);
}
catch(Exception e){};
};
};
setVisible(false);
};
public void initialize(){
setTitle("Game Tutorial");
setSize(windowWidth, windowHeight);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
};
public void update(){
if (input.isKeyDown(KeyEvent.VK_RIGHT)){
x += 5;
}
if (input.isKeyDown(KeyEvent.VK_LEFT)){
x -= 5;
}
};
public void draw(){
Color white = Color.WHITE;
Color black = Color.BLACK;
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.WHITE);
bbg.fillRect(0, 0, windowWidth, windowHeight);
bbg.setColor(Color.BLACK);
bbg.drawOval(x, 100, 20, 20);
g.drawImage(backBuffer, 0, 0, this);
};
}
输入处理程序:
package input;
import java.awt.Component;
import java.awt.event.*;
public class InputHandler implements KeyListener{
static boolean[] keys = new boolean[256];
public InputHandler(Component c){
c.addKeyListener(this);
}
public boolean isKeyDown(int keyCode){
if (keyCode > 0 && keyCode < 256){
keys[keyCode]=true;
return keys[keyCode];
}
return false;
}
public void keyPressed(KeyEvent e){
if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
keys[e.getKeyCode()] = true;
}
}
public void keyReleased(KeyEvent e){
if (e.getKeyCode() > 0 && e.getKeyCode() < 256){
keys[e.getKeyCode()] = false;
}
}
public void keyTyped(KeyEvent e){}
}
您没有为 InputHandler 输入赋值。
input=new InputHandler(argument);
您的 input 字段在使用之前从未获得值。你必须在使用它的方法之前初始化它。
类似于:
input = new InputHandler(whatever);
希望对您有所帮助
编辑
根据您的代码,用这样的字段声明和初始化替换字段声明可能就足够了:
InputHandler input = new InputHandler(this);