したがって、何らかの理由で、1000回以上のforループで実行しようとすると何らかの理由で常にNullPointerErrorを返すというコードがありますが、回。次のようにJava NullPointer ArrayQueueを使用してループ内で例外が発生する
ループがある:森は単に作成し、pは0又は1を開始すること、各セルの確率は0と1のint型のランダム2Dアレイを移入
double count = 0;
Forest f;
for (int i = 0; i < 1000; i++)
{
f = new Forest(20, 20, p);
if (f.breadthFirstSearch())
count++;
}
。幅優先探索(およびそれが使用するセルクラス)コードがforestGridは、int型の2次元配列であり、この、次のとおりです。
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();
}
、何の問題もないはずです。私はこれをうまく働かせようとしています。すべてのヘルプは非常にいただければ幸いです
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)
:何らかの理由でそれは常に最終的にここで私が得る例外だ
Cell currentCell = cellsToExplore.dequeue();
でbreathFirstSearchにnullを返します!
例外スタックトレースを投稿できますか? – jr593
@ jr593もちろん、追加しました。 「Cell currentCell = cellsToExplore.dequeue();」をポイントします。おそらく、前の行が何らかの未知の理由でnullを戻しているためです。 – GiantDwarf