使用ArrayQueue的for循环中的Java NullPointer异常

时间:2016-04-14 11:22:18

标签: java arrays loops queue

所以我有这个代码,当我尝试在1000次迭代的for循环中运行它时,由于某种原因经常给我一个NullPointerError,但是当它运行一次或不到1000次时它完全正常。

循环如下:

    double count = 0;
    Forest f;

    for (int i = 0; i < 1000; i++)
    {
        f = new Forest(20, 20, p);
        if (f.breadthFirstSearch())
            count++;
    }

其中,Forest只是创建并填充一个0和1的整数的随机2d数组,其中p是每个单元格开始为0或1的概率。 breadthFirstSearch(以及它使用的Cell类)代码是这样的,其中forestGrid是一个2d的int数组:

    public boolean breadthFirstSearch() {
    Queue<Cell> cellsToExplore = new ArrayQueue<>();

    for (int i = 0; i < width; i++)
        if (forestGrid[0][i] == 1)
            cellsToExplore.enqueue(new Cell(0, i));

    while (!cellsToExplore.isEmpty())
    {
        Cell currentCell = cellsToExplore.dequeue();
        currentCell.setBurning(true);

        int currentRow = currentCell.getRow();
        int currentColumn = currentCell.getColumn();

        forestGrid[currentRow][currentColumn] = 2;

        if (currentRow == height-1)
            return true;

        if (forestGrid[currentRow+1][currentColumn] == 1)
            cellsToExplore.enqueue(new Cell(currentRow+1, currentColumn));

        if ((currentRow > 0)&&(forestGrid[currentRow-1][currentColumn] == 1))
            cellsToExplore.enqueue(new Cell(currentRow-1, currentColumn));

        if ((currentColumn < width-1)&&(forestGrid[currentRow][currentColumn+1] == 1))
            cellsToExplore.enqueue(new Cell(currentRow, currentColumn+1));

        if (((currentColumn > 0)&&forestGrid[currentRow][currentColumn-1] == 1))
            cellsToExplore.enqueue(new Cell(currentRow, currentColumn-1));
    }

    return false;
}

private static class Cell {

    boolean burning;
    int row, column;

    public Cell(int r, int c) {
        row = r;
        column = c;
        burning = false;
    }

    public boolean isBurning() {
        return burning;
    }

    public void setBurning(boolean b) {
        burning = b;
    }

    public int getRow() {
        return row;
    }

    public int getColumn() {
        return column;
    }
}

我的ArrayQueue是这样的:

public static final int CAPACITY = 1000;
private E[] data;
private int f = 0;
private int size = 0;

public ArrayQueue() {
    this(CAPACITY);
}

public ArrayQueue(int capacity) {
    data = (E[]) new Object[capacity];
}

public int size() {
    return size;
}

public boolean isEmpty() {
    return size == 0;
}

public void enqueue(E e) throws IllegalStateException {
    if (size == data.length)
        resize();

    int rear = (f + size) % data.length;
    data[rear] = e;
    size++;
}

public E dequeue() {
    if (isEmpty()) {
        return null;
    }

    E answer = data[f];
    data[f] = null;
    f = (f + 1) % data.length;
    size--;

    return answer;
}

public E first() {
    if (isEmpty())
        return null;

    return data[f];
}

private void resize() {
    E[] temp = (E[]) new Object[data.length * 2];
    // System.out.println("Resizing array to " + temp.length + ".");
    for (int i = 0; i < data.length; i++)
        temp[i] = data[i];
    data = temp;
}

public String toString() {
    StringBuilder sb = new StringBuilder("(");
    int k = f;
    for (int i = 0; i < size; i++) {
        if (i > 0)
            sb.append(", ");

        sb.append(data[k]);
        k = (k + 1) % data.length;
    }
    sb.append(")");
    return sb.toString();
}

据我所知,不应该有任何问题,而且我最终想要让它发挥作用。出于某种原因,它总是最终会在

的breathFirstSearch中返回null
    Cell currentCell = cellsToExplore.dequeue();

以下是我得到的例外情况:

Exception in thread "main" java.lang.NullPointerException
at algorithms.Forest.breadthFirstSearch(Forest.java:71)
at driver.FireProbability.computeProbabilty(FireProbability.java:18)
at driver.FireProbability.highestProbability(FireProbability.java:34)
at driver.FireProbability.main(FireProbability.java:8)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里

E answer = data[f];
data[f] = null;

answer指向与data[f]相同的位置因此,当您为空data[f]时,您将answer指向的位置为空,并返回此位置。