簡體   English   中英

訪問數組成員時拋出NullPointerException

[英]NullPointerException being thrown whenever array member is accessed

我遇到一個異常問題,每次嘗試執行以下代碼時都會拋出異常。

下面是驅動程序,下面我將為您提供RoomplayerEnters方法的構造函數。

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

房間的構造者

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方法

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

每當在驅動程序中調用playerEnters方法時拋出NullPointerException ,然后在調用getMap方法時再次拋出NullPointerException

忽略其他一切,這就是問題所在:

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

您創建一個Room數組,但不要在該數組中創建任何Room對象。 因此dungeon[playerPos]null

您需要使用Room對象填充該數組。

for (int i = 0; i < numRooms; i++)
{
    dungeon[i] = new Room();
}

您需要實例化房間。

 Room dungeon[] = new Room[numRooms];
 dungeon[0]=new Room();
 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM