Java 是什么导致我的NullPointerException?

Java 是什么导致我的NullPointerException?,java,nullpointerexception,Java,Nullpointerexception,我的程序编译得非常完美,但每当我试图运行它时,它都会抛出一个NullPointerException。我尝试搜索这个,发现错误与程序试图使用它时某个值为null有关,但我重新检查了所有内容,结果为空 以下是错误: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(U

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

以下是错误:

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)
守则:

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;
  }
}

static
关键字添加到
main
方法中,使应用程序具有有效的入口点

public static void main (String [] args) {
编辑:

完成此更改后,创建
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);
    } ...

将所有方法更改为static,并且不应使用“==”来比较组合类型

我们是否可以看到错误,包括错误发生的行。请不要尝试将字符串与“==”进行比较。看,我想这是创建以来的第一天,字符串上没有
=
:(@user2928683:这根本不在您的代码中,看起来您正在使用自定义Java编译器。如果您尝试使用普通Java编译器编译代码并运行它,会发生什么情况?
public void main(String[]args)
应该是
publicstaticvoidmain(String[]args)
您是如何看到的:|添加有关使用
equals()
方法的信息,而不是
=
方法。用户肯定也需要知道这一点。:)现在main是静态的,作为一个副作用,所有其他方法也需要是静态的,尽管这会破坏其他所有方法,因为main的编写方式似乎不是静态的。我重新命名了现有的main并重新创建了一个新的real main。谢谢,这修复了它!使所有的方法都是静态的,并且运行起来就像一个CHAMP。@ USER 29 868 3YW,考虑如果你发现它有用的话:
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;
  }
}