Java 创建游戏时获取NullPointerException
我正在创造一个游戏。它不会在Eclipse中抛出错误,但当我在调试模式下运行它时,它会在第行抛出一个Java 创建游戏时获取NullPointerException,java,Java,我正在创造一个游戏。它不会在Eclipse中抛出错误,但当我在调试模式下运行它时,它会在第行抛出一个NullPointerException: System.out.println("Stone x: " + blocks.get(BlockRectangle.getByID(rectID)).getX() + " y: " + blocks.get(BlockRectangle.getByID(rectID)).getY()); Game.java: package lt.projectur
NullPointerException
:
System.out.println("Stone x: " + blocks.get(BlockRectangle.getByID(rectID)).getX() + " y: " + blocks.get(BlockRectangle.getByID(rectID)).getY());
Game.java:
package lt.projecturanium;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.HashMap;
import javax.swing.JFrame;
import lt.projecturanium.blocks.Block;
import lt.projecturanium.blocks.BlockRectangle;
import lt.projecturanium.entity.Player;
@SuppressWarnings("unused")
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private static JFrame _frame;
public static Game _instance;
private static final String TITLE = "Project Uranium";
private static final int WIDTH = 650;
private static final int HEIGHT = WIDTH * 3 / 4;
private static final int UPDATE_RATE = 50;
private static final int RENDER_RATE = 100;
public static HashMap<Block, Coordinates> blocks = new HashMap<Block, Coordinates>();
public int rectx = 0;
public int recty = 0;
public int rectID = 0;
public boolean hitted = false;
public float interpolation;
public static final Dimension SIZE = new Dimension(WIDTH, HEIGHT);
private Thread _thread;
private boolean _running;
private int _totalTicks = 0;
private int _tps = 0;
private int _fps = 0;
public Game()
{
_instance = this;
setPreferredSize(SIZE);
setMinimumSize(SIZE);
setMaximumSize(SIZE);
_frame = new JFrame(TITLE);
_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
_frame.setLayout(new BorderLayout());
_frame.add(_instance, BorderLayout.CENTER);
_frame.pack();
_frame.setResizable(false);
_frame.setLocationRelativeTo(null);
_frame.setVisible(true);
createBufferStrategy(2);
blocks.put(new Block(new BlockRectangle(200)), new Coordinates(30, 50));
}
public synchronized void start()
{
_running = true;
_thread = new Thread(this, TITLE+"_main");
_thread.start();
}
public synchronized void stop()
{
_running = false;
if (_thread != null)
{
try {
_thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
super.paint(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
g2.drawString("FPS: " + _fps + "\n TPS: " + _tps, 10, 10);
if (hitted)
{
recty = 0;
rectx += 21;
rectID++;
blocks.put(new Block(new BlockRectangle(rectID)), new Coordinates(rectx, recty));
hitted = false;
}
recty++;
g2.drawImage(Player.getTexture(), 60, 60, null);
g2.drawRect(rectx, recty, 20, 20);
g2.setColor(new Color(101, 67, 33));
g2.fillRect(0, 430, getWidth(), getHeight());
g2.setColor(new Color(0, 100, 0));
g2.fillRect(0, 420, getWidth(), 10);
g2.setColor(Color.BLACK);
if (recty == (419 - 20))
{
hitted = true;
}
}
public void run() {
double lastUpdateTime = System.nanoTime();
double lastRenderTime = lastUpdateTime;
final int ns = 1000000000;
final double nsPerUpdate = (double) ns / UPDATE_RATE;
final double nsPerRender = (double) ns / RENDER_RATE;
final int maxUpdatesBeforeRender = 5;
int lastSecond = (int) (lastUpdateTime / ns);
int tickCount = 0;
int renderCount = 0;
while (_running) {
long currTime = System.nanoTime();
int tps = 0;
while ((currTime - lastUpdateTime) > nsPerUpdate && tps < maxUpdatesBeforeRender) {
update();
tickCount++;
_totalTicks++;
tps++;
lastUpdateTime += nsPerUpdate;
interpolation = Math.min(1.0F, (float) ((currTime - lastUpdateTime) / nsPerUpdate));
render(interpolation, getGraphics());
}
if (currTime - lastUpdateTime > nsPerUpdate) {
lastUpdateTime = currTime - nsPerUpdate;
}
if (currTime - lastRenderTime == maxUpdatesBeforeRender + 1)
{
render(interpolation, getGraphics());
}
renderCount++;
lastRenderTime = currTime;
int currSecond = (int) (lastUpdateTime / ns);
if (currSecond > lastSecond) {
_tps = tickCount;
_fps = renderCount;
tickCount = 0;
renderCount = 0;
lastSecond = currSecond;
_frame.setTitle(TITLE + " | TPS: " + _tps + " | FPS: "+ _fps);
}
while (currTime - lastRenderTime < nsPerRender && currTime - lastUpdateTime < nsPerUpdate) {
Thread.yield();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
currTime = System.nanoTime();
}
}
}
public void update()
{
_frame.pack();
}
public void render(float interp, Graphics g)
{
BufferStrategy myStrategy = getBufferStrategy();
Graphics gra = myStrategy.getDrawGraphics();
paint(gra);
g.dispose();
myStrategy.show();
//System.out.println("Grass x: " + blocks.get("grass").getX() + " y: " + blocks.get("grass").getY());
System.out.println("Stone x: " + blocks.get(BlockRectangle.getByID(rectID)).getX() + " y: " + blocks.get(BlockRectangle.getByID(rectID)).getY());
}
}
错误:
Thread [Project Uranium_main] (Suspended (exception NullPointerException))
Game.render(float, Graphics) line: 186
Game.run() line: 139
Thread.run() line: not available
通过查看可能为null的单个值开始调试。您可以使用此块作为示例:
BlockRectangle blockRectangle = BlockRectangle.getByID(rectID);
System.out.println("BlockRectangle: " + blockRectangle);
Coordinates coordinates= blocks.get(blockRectangle);
System.out.println("Coordinates: " + coordinates);
System.out.println("X: " + coordinates.getX());
System.out.println("Y: " + coordinates.getY());
一旦确定了什么是空值,您就可以开始回溯代码,以确定为什么没有一个您期望的值集。空指针异常是指代码试图使用一个值执行某些操作,但该值为空。值为null通常是因为它不是实例化的
Object o=new Object
,或者是从数据源提取的,如果所需记录不存在,它将返回null。看
在您的例子中,在System.out.println(“Stone x:+blocks.get(BlockRectangle.getByID(rectID)).getX()+“y:+blocks.get(BlockRectangle.getByID(rectID)).getY()行中得到一个NPE代码>因为映射返回空的BlockRectangle对象
关于您对@GamerJosh答案的评论,您的错误在坐标中,我猜这是因为您从getById()
返回了一个空块矩形。请这样想:您从不增加/更改您的rectId字段,而是添加第一个Id为200
的对象。除此之外,在paint()方法中添加Id为0
的新块。但是,如果在其他块出现在地图中之前先看到这个日志语句,会发生什么呢?如果其中唯一的记录Id为200
,那么您的地图会为Id0
返回什么
也就是说,我不认为保存所有块矩形的地图是最好的程序设计。相反,考虑在主类中有一个变量来跟踪所有这些。
另外,你的英语很好,很高兴知道你很小就开始编程了!另外,由于您正在制作一个游戏,我想指出一个网站,在那里您可以询问游戏特定的问题,如游戏设计或编程问题,游戏制作者可能会对这些问题有更深入的了解。无论您在何处运行,您的代码都会抛出NullPointerException。只是在线程的run
方法中抛出此异常只能通过两种方式捕获:要么通过try{…}catch(NullPointerException e){…}
显式捕获,要么通过定义线程的uncaughtExceptionHandler()
来捕获
run
方法不一定是您的run
,而是定义为调用绘制渲染的方法
根据您预期的运行时异常,您可以定义这样的异常处理程序,也可以消除异常的原因。在您的例子中,get
方法之一没有找到元素并返回null。多么奇怪的编程
然而,问题似乎就在这里:
1) 在类Game
中,您有一个名为blocks的类型为HashMap
的静态字段。您正在将键的类块实例插入到该映射中。对应行为:
blocks.put(new Block(new BlockRectangle(rectID)), new Coordinates(rectx, recty));
rects.put(id, this);
2) 在同一行中创建类型为BlockRectangle
的instance时,构造函数将构造中的对象作为键放入名为rects的类型为HashMap
的静态字段中。对应行为:
blocks.put(new Block(new BlockRectangle(rectID)), new Coordinates(rectx, recty));
rects.put(id, this);
3) 然后,你试图通过打电话来获得阻塞
blocks.get(BlockRectangle.getByID(rectID))
内部表达式BlockRectangle.getByID(rectID)
将返回在映射矩形中查找的BlockRectangle
实例。现在,在该实例中,将在地图块中执行查找,但这仅将Block
s存储为键,而不是BlockRectangle
s(请参见第1点)
如果在Block
和BlockRectangle
中没有任何equals
(和hashCode
)方法,您将无法获得进一步的结果。但是在这些类中的任何一个中实现这些方法都会使情况变得更糟,因为将块与块矩形进行比较不是一个好主意
你应该彻底重新设计你的软件。你能把错误输出添加到问题中吗?我想你知道我正在创建一个游戏。
--到目前为止还不知道,但会记下来。你说“它不会在Eclipse中抛出错误”是什么意思?您的意思是Eclipse不报告编译错误吗?您正在Eclipse中调试它,对吗?是的,我们可以使用堆栈跟踪。它不会在Eclipse中抛出错误,因为这是一个运行时错误,而不是编译时错误。代码在语法上是可以编译的,但是当它运行时,在某个地方有一个逻辑错误。此特定错误意味着该行中的一项为null
,无法使用。它可能是变量blocks
(或者更可能是blocks
上的.get()
方法返回的任何内容),但我不能从这里确定。看起来您正在尝试在分配任何变量之前使用它。好的,我会检查:)谢谢您的回答:)您的映射是静态的,但是您的BlockRectangle类不是。getByID()类是静态的?嗯,BlockRectangle不是空的:BlockRectangle:lt.ProjectUrium.blocks。BlockRectangle@15e538eIF你是BlockRectange不是空的,然后,将块矩形与坐标关联时会出现问题。要了解发生这种情况的原因,请查看@Seelenvirtuose的答案。我确定这是坐标。这是一个很好的第一步——现在您知道,当调用blocks.get(blockRectangle)时,您将返回一个空值。由于您使用的是HashMap,因此有两个可能的原因导致出现null:键(blockRectangle)不存在,或者键存在,但被分配了null值。您可以通过迭代地图并打印它包含的内容来确定这一点。你也会想要