Java 是什么导致了我的 NullPointerException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19639150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:01:25  来源:igfitidea点击:

What is causing my NullPointerException?

javanullpointerexception

提问by user2928683

My program compiles perfectly, but whenever I try to run it throws a NullPointerException. I tried searching this up, and found that the error relates to some value being null while the program is trying to use it, but I re-checked everything and came up blank.

我的程序编译完美,但是每当我尝试运行它时,它都会抛出 NullPointerException。我尝试搜索它,发现该错误与程序尝试使用它时某些值为 null 的值有关,但我重新检查了所有内容,结果为空白。

Here's the error:

这是错误:

java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27??2)

The code:

编码:

import javax.swing.*;
import java.util.Scanner;

public class WorldsMostBoringGame
{ 
  public void main (String [] args)
  {    
    System.out.println("You are in a room with a locked door and a key.");

     Scanner keyboard = new Scanner(System.in);
     boolean hasKey = false, doorOpen = false, amIDoneYet = false, monsterAlive = true;

    while (!amIDoneYet)
    {
      String userInput = keyboard.nextLine();

      if (userInput == "look around")
        LookAround(hasKey);
      else if (userInput == "get key")
        GetKey(hasKey, monsterAlive);
      else if (userInput == "open door")
        OpenDoor(doorOpen, hasKey, amIDoneYet);
      else if (userInput == "kill monster")
        KillMonster(monsterAlive);
      else
        System.out.println(userInput+ " is not a recognized command.");
    }
  }

  public boolean GetKey(boolean hasKey, boolean monsterAlive)
  {
    if (hasKey == false && monsterAlive == false)
      System.out.println("You pick up the key.");
    else if (hasKey == true && monsterAlive == false)
      System.out.println("You already picked up the key.");
    else if (monsterAlive == true)
    {
      System.out.println("You must kill the monster first.");
      return hasKey = false;
    }

    return hasKey = true;
  }

  public void LookAround(boolean hasKey)
  {
    if (!hasKey)
      System.out.println("You are in a room with a locked door and a key.");
    else
      System.out.println("You are in a room with a locked door. You have a key.");
  }

  public boolean OpenDoor(boolean doorOpen, boolean hasKey, boolean amIDoneYet)
  {
    if (hasKey)
    {
      System.out.println("You unlock the door. Game over. You win.");
      amIDoneYet = true;
      return doorOpen;
    }
    else
    {
      System.out.println("The door is locked. Find a key.");
      return doorOpen = false;
    }
  }

  public boolean KillMonster(boolean monsterAlive)
  {
    System.out.println("You kill the monster.");
    return monsterAlive = false;
  }
}

回答by Reimeus

Add a statickeyword to the mainmethod so the application has a valid entry point

staticmain方法添加关键字,以便应用程序具有有效的入口点

public static void main (String [] args) {

Edit:

编辑:

Once this change is made create an instance of WorldsMostBoringGamesuch that the instance methods can be invoked:

进行此更改后,创建一个实例,WorldsMostBoringGame以便可以调用实例方法:

WorldsMostBoringGame game = new WorldsMostBoringGame(); // create this instance
while (!amIDoneYet) {
   String userInput = keyboard.nextLine();
    if ("look around".equals(userInput)) { // use String.equals
       game.lookAround(hasKey);
    } ...

回答by Junjie

import javax.swing.*;
import java.util.Scanner;

public class WorldsMostBoringGame
{ 
  public static void  main(String [] args)
  {    
    System.out.println("You are in a room with a locked door and a key.");

     Scanner keyboard = new Scanner(System.in);
     boolean hasKey = false, doorOpen = false, amIDoneYet = false, monsterAlive = true;

    while (!amIDoneYet)
    {
      String userInput = keyboard.nextLine();

      if (userInput.equals("look around"))
        LookAround(hasKey);
      else if (userInput.equals("get key"))
        GetKey(hasKey, monsterAlive);
      else if (userInput.equals("open door"))
        OpenDoor(doorOpen, hasKey, amIDoneYet);
      else if (userInput.equals("kill monster"))
        KillMonster(monsterAlive);
      else
        System.out.println(userInput+ " is not a recognized command.");
    }
  }

  public static boolean GetKey(boolean hasKey, boolean monsterAlive)
  {
    if (hasKey == false && monsterAlive == false)
      System.out.println("You pick up the key.");
    else if (hasKey == true && monsterAlive == false)
      System.out.println("You already picked up the key.");
    else if (monsterAlive == true)
    {
      System.out.println("You must kill the monster first.");
      return hasKey = false;
    }

    return hasKey = true;
  }

  public static void LookAround(boolean hasKey)
  {
    if (!hasKey)
      System.out.println("You are in a room with a locked door and a key.");
    else
      System.out.println("You are in a room with a locked door. You have a key.");
  }

  public static boolean OpenDoor(boolean doorOpen, boolean hasKey, boolean amIDoneYet)
  {
    if (hasKey)
    {
      System.out.println("You unlock the door. Game over. You win.");
      amIDoneYet = true;
      return doorOpen;
    }
    else
    {
      System.out.println("The door is locked. Find a key.");
      return doorOpen = false;
    }
  }

  public static boolean KillMonster(boolean monsterAlive)
  {
    System.out.println("You kill the monster.");
    return monsterAlive = false;
  }
}

change all the method to static, and "==" should n't used to compare composed type

将所有方法更改为静态,并且“==”不应该用于比较组合类型