每当数组成员访问抛出NullPointerException [英] NullPointerException being thrown whenever array member is accessed

查看:143
本文介绍了每当数组成员访问抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有那个不断被抛出每次我试图执行以下code时异常的问题。

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

下面是驱动程序,下面我会给你客房 playerEnters 方法构造。

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

构造室

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

playerEnters method

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

NullPointerException异常每当 playerEnters 方法被调用驱动程序被抛出,然后再当的GetMap 方法被调用。

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

推荐答案

忽略一切,这就是问题所在:

Ignoring everything else, this is the problem:

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

您创建一个客房数组,但不要创建阵列中的任何客房的对象。因此,地牢[playerPos]

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

您需要填充客房对象数组。

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

这篇关于每当数组成员访问抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆