2016-04-14 6 views
0

so habe ich diesen Code, der mir aus irgendeinem Grund ständig einen NullPointerError gibt, wenn ich es in einer for-Schleife über 1000 Iterationen ausführen, aber es funktioniert einwandfrei, wenn einmal oder unter 1000 ausgeführt mal.Java NullPointer Ausnahme innerhalb von for-Schleife mit ArrayQueue

Die Schleife ist wie folgt:

double count = 0; 
    Forest f; 

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

Wo Wald einfach erstellt ist und füllt einen zufälligen 2D-Array mit ints von 0 und 1 ist, wobei p die Wahrscheinlichkeit jeder Zelle ist 0 oder 1 zu starten. Die Breitensuche (und die Zell Klasse nutzt) Code ist dies, wo forestGrid ist eine 2D-Array von ints:

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

und meine ArrayQueue dies:

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

Soweit ich das beurteilen kann, Es sollte keine Probleme geben, und ich bin am Ende meines Wissens mit dem Versuch, dies zur Arbeit zu bringen. Aus irgendeinem Grund wird es schließlich immer null zurück in breathFirstSearch bei

Cell currentCell = cellsToExplore.dequeue(); 

Hier ist die Ausnahme, die ich erhalten:

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) 

Jede Hilfe wäre sehr geschätzt!

+0

Können Sie die Ausnahme stacktrace posten? – jr593

+0

@ jr593 Sicher, nur hinzugefügt. Es zeigt auf "Cell currentCell = cellsToExplore.dequeue();" als der Fehler, wahrscheinlich, weil die vorherige Zeile aus einem unbekannten Grund null zurückgibt. – GiantDwarf

Antwort

0

denke ich, das Problem hier ist

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

answer Punkte an der gleichen Stelle wie data[f] Wenn Sie also data[f] null Sie null die Lage durch answer spitz, und dies zurückgegeben wird.

+0

Nun, da ich das Element in Daten speichern [f] zu beantworten, sollte es egal sein, was im Array mit Antwort passiert. Wie auch immer, ich habe es herausgefunden - es war meine Methode zur Größenanpassung, die meine Zeiger dazu brachte, auf den falschen Ort zu zeigen. – GiantDwarf

Verwandte Themen