Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 游戏显示空指针异常_Java_Nullpointerexception - Fatal编程技术网

Java 游戏显示空指针异常

Java 游戏显示空指针异常,java,nullpointerexception,Java,Nullpointerexception,我试图显示一个黑色窗口,但在第72行和第43行,我一直得到空指针异常。这是我的视频游戏的基础,自从我刚接触java以来,我一直在使用教程来帮助我。它一开始是一个无法访问的代码错误,但我通过返回修复了这个错误,然后这个问题立即出现了,有什么帮助吗? 代码: 如果它为空,那么您似乎正在使用一种常见的创建模式。但是,这里您将bs设置为getBufferStrategy(),如果为null,则创建它。让我们假设函数是成功的。bs仍然是空的 BufferStrategy bs = this.getBuff

我试图显示一个黑色窗口,但在第72行和第43行,我一直得到空指针异常。这是我的视频游戏的基础,自从我刚接触java以来,我一直在使用教程来帮助我。它一开始是一个无法访问的代码错误,但我通过返回修复了这个错误,然后这个问题立即出现了,有什么帮助吗? 代码:


如果它为空,那么您似乎正在使用一种常见的创建模式。但是,这里您将bs设置为getBufferStrategy(),如果为null,则创建它。让我们假设函数是成功的。bs仍然是空的

BufferStrategy bs = this.getBufferStrategy(); 
if (bs == null){
       this.createBufferStrategy(3); 
       //bs still null
}
您需要重新尝试将bs设置为某个值

BufferStrategy bs = this.getBufferStrategy(); 
if (bs == null) {
       this.createBufferStrategy(3); 
       bs = this.getBufferStrategy(); 
}
所有这些都假设createBufferStrategy不会失败。如果可以的话,你必须决定在那种情况下该怎么办

其他注释
您的if声明也有保留;在里面。这使得它成为一个空的if语句

在处理bs==null时,您肯定缺少一个else

    if(bs == null) //there should not be a semi-colon here;
    {
        this.createBufferStrategy(3);
    }
    else
    {
        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0,0, getWidth(), getHeight());

        g.dispose();
        bs.show();
     }

但是,您是否也可以标记第72行和第43行;我注意到,如果bs==null,您将创建一个缓冲区策略,但您不会再次尝试将bs设置为等于itA
BufferStrategy
,只有在
画布
显示时才能创建
(或连接到本机对等方)。你的线程可能会抢占它…你的if语句也有一个错误;在里面
    if(bs == null) //there should not be a semi-colon here;
    {
        this.createBufferStrategy(3);
    }
    else
    {
        Graphics g = bs.getDrawGraphics();

        g.setColor(Color.black);
        g.fillRect(0,0, getWidth(), getHeight());

        g.dispose();
        bs.show();
     }