0

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.

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; } } 
12
  • 8
    Could we see the error, including the line at which it occures Commented Oct 28, 2013 at 15:44
  • 4
    Additionally, don't try to compare strings with "==". See tinyurl.com/so-java-string-equality Commented Oct 28, 2013 at 15:45
  • 1
    I thought this is the first day since creation there is no == on Strings :( Commented Oct 28, 2013 at 15:45
  • 1
    @user2928683: Well that's not in your code at all, and it looks like you're using a custom Java compiler. What happens if you try to compile that code with a normal Java compiler and run it? Commented Oct 28, 2013 at 15:45
  • 1
    public void main (String [] args) should be public static void main (String [] args) Commented Oct 28, 2013 at 15:47

2 Answers 2

15

Add a static keyword to the main method so the application has a valid entry point

public static void main (String [] args) { 

Edit:

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

WorldsMostBoringGame game = new WorldsMostBoringGame(); // create this instance while (!amIDoneYet) { String userInput = keyboard.nextLine(); if ("look around".equals(userInput)) { // use String.equals game.lookAround(hasKey); } ... 
Sign up to request clarification or add additional context in comments.

5 Comments

How did you see that :|
Do add the info about using equals() method instead of == also. The user definitely needs to know that as well. :) And now that main is static, as a side effect, all the other methods need to be static as well.
Although this breaks everything else because main is written as if its not static. I renamed the existing main and recreated a new real main
Thanks, this fixed it! Made all the methods static, and runs like a champ.
@user2928683 I recommend creating an instance of WorldsMostBoringGame to avoid making those methods static
0
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.