2016-02-29 227 views
0

这是我的代码我遇到的问题是一个数组越界的错误。望着代码我似乎无法弄清楚的问题是什么空指针异常

//名称:凯文·兰德尔 //用户名: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(); 
} 

}

+0

你能发表整个代码吗? –

+0

你不需要发布整个代码。问题很明显。 –

回答

2
dungeon = new Cell[width][height]; 

这创建Cell对象,用null初始化的两个dimensionnal阵列。所以每一次访问都会给你一个例外。您需要创建Cell对象并自己填充它们。