我在此代码上不断收到 Java.Lang.NullPointerException:
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
}
Graphics g = bs.getDrawGraphics();
g.dispose();
bs.show();
}
有人能告诉我我做错了什么吗?
最佳答案
即使您调用 this.createBufferStrategy(3);
,您的 bs
变量仍然未分配。
创建后需要读回它:
if(bs == null){
this.createBufferStrategy(3);
bs = this.getBufferStrategy();
}
最好添加一个检查,以确保在调用 createBufferStrategy
之后返回一个非 null:
this.createBufferStrategy(3);
bs = this.getBufferStrategy();
if (bs == null) throw new IllegalStateException("Buffered structure is not created.");
关于java - 为什么我不断收到 Java.Lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094151/