Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:18:35+00:00 2026-05-26T23:18:35+00:00

I am having an issue with an exception that keeps being thrown every time

  • 0

I am having an issue with an exception that keeps being thrown every time I attempt to execute the following code.

Below is the driver, and below that I will give you the constructor for Room and the playerEnters method.

import java.util.Random;
import java.util.Scanner;

public class ZorkPlayer
{

    public static void main (String [ ] args)
    {
        // create a start screen followed by introduction
        int choice = 0;
        while(choice != 3)
        {
            choice = menu();
            switch (choice)
            {
                case 1:
                    //begin new game
                    newGame();
                    break;
                case 2:
                    //change difficulty level
                    break;
                case 3:
                    //exit the program
                    break;
                default: 
                    //invalid choice
                    break;
            }
        }
    }

    /**
     * Creates a menu and gets a choice from the user <br>        
     */
    public static int menu ()
    {
        Scanner kb = new Scanner(System.in);
        System.out.println("Welcome to Zork!");
        System.out.println("What would you like to do?");
        System.out.println("\t1- Start a new game");
        System.out.println("\t2- Choose difficulty level (not yet available)");
        System.out.println("\t3- Exit the program");
        int choice = kb.nextInt();
        return choice;
    }

    public static void newGame()
    {
        final int DEFAULT_ROOMS = 5;
        Random rng = new Random ();
        int numRooms = rng.nextInt(5) + DEFAULT_ROOMS;
        int playerPos = 0;
        Room dungeon[] = new Room[numRooms];

        dungeon[playerPos].playerEnters();
        for (int i = 0; i < dungeon.length; i++)
        {
            System.out.print (dungeon[i].getMap ( ));
        }
    }
}

Constructor for Room

private int monster = 0; //initializes with no monster spawned
private int player = 0; //initializes with no player present
private int weapon = 0; //initializes with no weapon present

public Room()
{
    //creates a new room and determines whether the new room has a monster or not
    Random rng = new Random();
    int monsterSpawn = rng.nextInt (2); //determines whether or not a monster will spawn in the room
    if (monsterSpawn == 0)
    {
        new Monster(); //if a monster is spawned, creates a new monster and sets the value of monster to present
        setMonster(1);  
    }
    else
        setMonster(0); //otherwise leaves the value of monster to default
    setPlayer(0); //sets the presence of player to false
    setWeapon(0); //sets the presence of weapon to false
}

playerEnters method

public void playerEnters()
{
    setPlayer(1);
}

The NullPointerException is thrown whenever the playerEnters method is called in the driver, and then again when the getMap method is called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:18:35+00:00Added an answer on May 26, 2026 at 11:18 pm

    Ignoring everything else, this is the problem:

    Room dungeon[] = new Room[numRooms];
    dungeon[playerPos].playerEnters();
    

    You create a Room array, but don’t create any Room objects in that array. Therefore dungeon[playerPos] is null.

    You need to populate that array with Room objects.

    for (int i = 0; i < numRooms; i++)
    {
        dungeon[i] = new Room();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using code similar to whats below with the exception that the thread is
Having an issue here that I have tried everything I can think of but
I'm having an issue with a query that currently uses LEFT JOIN weblog_data AS
I'm having an issue with a standard ASP.NET page that has a TextBox and
I'm having a issue with the following scenario on Windows Forms: I created a
We are having an issue when using NHibernate with distributed transactions. Consider the following
I am having the standard issue timeout exception in my production environment whereby, after
I am having an issue serializing a c# class to an XML file that
I'm having a little issue with Doctrine. Im getting the following error: PHP Fatal
Having an issue with CheckedTextView that I can't seem to find a solution. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.