gpt4 book ai didi

java - 空指针异常

转载 作者:行者123 更新时间:2023-12-02 03:53:05 26 4
gpt4 key购买 nike

这是我的代码我遇到的问题是数组越界错误。查看代码我似乎无法弄清楚问题出在哪里

//姓名:凯文·兰德尔 //用户名:KRandall //描述:该程序将创建地牢和相关声音

public class Dungeon
{
private Cell[][] dungeon;
private Config config;
private int width;
private int height;
private Hero hero;
private Stats stats;
// Construct a new dungeon of the specified size.
// NOTE: The dungeon starts out completely empty and won't be usable
// until other methods have been run the constructed object:
// 1) set() to initialize each Cell in the dungeons 2D array
// 2) addHero() to put a hero in the dungeon
// 3) addItem() to add keys and gems to the dungeon
public Dungeon(Config config, int width, int height)
{
this.config = config;
this.width = width;
this.height =height;
dungeon = new Cell[width][height];
stats = new Stats();
}

// Adds the specified hero to the dungeon
public void addHero(Hero hero)
{
this.hero = hero;

}

public int getHeight()
{
return height;
}
public int getWidth ()
{
return width;
}
public int getCount ( Item item)
{
int count = 0;
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
dungeon[i][j].containsItem(item);
if (dungeon[i][j].containsItem(item))
count++;
}

}
return count;
}
public double getElapsedTime()
{
return stats.elapsedTime();

}
// Setup a given grid location based on a string:
// "W" = wall
// "S" = secret passage
// "D" = closed door
// "O" = open door
// "-" = dirt floor
public void set(int cellX, int cellY, String str)
{
String walls = config.wallImage();
String pass = config.passageImage();
String cDoor = config.closedDoorImage();
String oDoor = config.openDoorImage();
String floor = config.passageImage();
String [] rock = config.rockImages();
switch (str)
{
case "R": dungeon[cellX][cellY] = new CellRock(config);
break;
case "W": dungeon[cellX][cellY] = new Cell(walls);
break;
case "S": dungeon[cellX][cellY] = new CellPassage(pass);
break;
case "D": dungeon[cellX][cellY] = new CellDoor(oDoor,cDoor,true);
break;
case "O": dungeon[cellX][cellY] = new CellDoor(oDoor,cDoor,false);
break;
case "-": dungeon[cellX][cellY] = new CellPassage(floor);
break;
}
hero.collectItem(dungeon[cellX][cellY].treasure);
dungeon[cellX][cellY].treasure = Item.NONE;

}


// Add the specified item to a random location in the dungeon.
// The item can only be added to a cell that is passable (i.e.
// not a wall or a locked door). The location must not already
// have an item and items can't be added on top of the hero.
public void addItem(Item item)
{
boolean placed = false;
while(!placed){

int xPos = (int)(Math.random()* width);
int yPos = (int)(Math.random()* height);
if (!(hero.getX() == xPos && hero.getY() == yPos)){
if(dungeon[xPos][yPos].attemptToAdd(item)){
placed = true;
}

}

}

}

// Make the Hero execute the specified action.
// Currently this is attempting to move north, south, east, or west.
// If the hero succeeds in moving, play the walking sound.
// If the action would take the hero into an impassable cell OR
// off the dungeon grid, play the hit wall sound.
public void execute(Action action)
{
int x = hero.getX();
int y = hero.getY();

switch (action){
case INVALID:
break;
case NORTH: y++;if (y < height && dungeon[x][y].attemptToMove(hero)){StdAudio.play(config.walkSound());hero.setLocation(x, y);}
else StdAudio.play(config.hitWallSound());
break;
case SOUTH: y--;if (y >=0 &&dungeon[x][y].attemptToMove(hero)){StdAudio.play(config.walkSound());hero.setLocation(x, y);}
else StdAudio.play(config.hitWallSound());
break;
case EAST:x++; if (x < width && dungeon[x][y].attemptToMove(hero)){StdAudio.play(config.walkSound());hero.setLocation(x, y);}
else StdAudio.play(config.hitWallSound());
break;
case WEST:x--; if (x >= 0 && dungeon[x][y].attemptToMove(hero)){StdAudio.play(config.walkSound());hero.setLocation(x, y);}
else StdAudio.play(config.hitWallSound());
break;
}

}

// Draw all the cell tiles that make up the dungeon.
// Also draws the hero.
public void draw()
{
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
dungeon[i][j].draw(config, i, j);
}
}
hero.draw();

StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.text(64.0,64.0 * height - 32.0,"Keys: " + hero.numKey);
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.text(192.0,64 * height - 32.0,"Gems: " + hero.numGem);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.text(320.0, 64 * height - 32.0,"Axe: " + hero.numAxe);
}

// Test program for Dungeon, feel free to change for your own testing needs
public static void main(String [] args)
{
// The config object stores defaults for sizes, sounds, and images
Config config = new Config();

// Setup a fake 4 x 3 dungeon
final int WIDTH = 4;
final int HEIGHT = 3;
Dungeon dungeon = new Dungeon(config, WIDTH, HEIGHT);
StdDraw.setCanvasSize(WIDTH * config.cellPixels(), HEIGHT * config.cellPixels());
StdDraw.setXscale(0, WIDTH * config.cellPixels());
StdDraw.setYscale(0, HEIGHT * config.cellPixels());

// Create a dungeon like:
// W--W
// W---
// WDW-
dungeon.set(0, 0, "W");
dungeon.set(1, 0, "D");
dungeon.set(2, 0, "W");
dungeon.set(3, 0, "-");

dungeon.set(0, 1, "O");
dungeon.set(1, 1, "-");
dungeon.set(2, 1, "-");
dungeon.set(3, 1, "-");

dungeon.set(0, 2, "W");
dungeon.set(1, 2, "-");
dungeon.set(2, 2, "-");
dungeon.set(3, 2, "W");


Hero hero = new Hero(config, 1, 1);
dungeon.addHero(hero);

// Add 3 keys and 3 gems at random locations
for (int i = 0; i < 3; i++)
{
dungeon.addItem(Item.KEY);
dungeon.addItem(Item.GEM);
}

// Draw the sample dungeon
dungeon.draw();
}

}

最佳答案

dungeon = new Cell[width][height]; 

这将创建一个 Cell 对象的二维数组,并使用 null 进行初始化。所以每次访问都会给你一个异常(exception)。您需要创建 Cell 对象并自己用它们填充数组。

关于java - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35709949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com