2016-04-14 19 views
0

kullanarak döngü için istisna, bu kodun bir nedenle, sürekli olarak 1000 yinelemenin üzerinde bir döngü içinde çalıştırmayı denediğimde bana bir NullPointerError verir, ancak bir kez veya 1000'in altında çalıştırıldığında gayet iyi çalışır zamanlar. aşağıdaki gibiJava NullPointer ArrayQueue

döngüdür: Orman basitçe oluşturur ve p, 0 veya 1 başlatmak için olan, her bir hücre olasılığıdır 0 ve 1 arasında ints rastgele 2d dizi doldurur

double count = 0; 
    Forest f; 

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

. breadthFirstSearch (ve kullandığı Hücre sınıfı) forestGrid ints bir 2d dizidir kod şudur:

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

ve benim ArrayQueue şudur:

Bildiğim kadarıyla söyleyebilirim
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(); 
} 

, Herhangi bir sorun olmamalı ve ben bunu işe almak için çabalamaya çalışıyorum. Herhangi bir yardım son derece mutluluk duyacağız

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) 

: Nedense hep sonunda Benim elde istisna

Cell currentCell = cellsToExplore.dequeue(); 

de breathFirstSearch null dönecektir!

+0

Özel durum stacktrace'i gönderir misiniz? – jr593

+0

@ jr593 Elbette, ekledim. "Cell currentCell = cellsToExplore.dequeue();" işaret eder. hata olarak, muhtemelen önceki satırın bilinmeyen bir nedenden dolayı geri döndüğü için. – GiantDwarf

cevap

0

Sana data[f] boş zaman data[f] Bu nedenle, konum answer tarafından işaret boş olarak sorun burada aynı yere

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

answer puan olduğunu düşünüyorum ve bu döndürülmeden.

+0

Öğeyi veri [f] 'ye kaydetmek için kaydettiğime göre, dizide yanıtla ne olduğu önemli değil. Her iki durumda da, bunu anladım - işaretçilerimin yanlış yere işaret etmesine neden olan yeniden boyutlandırma yöntemimdi. – GiantDwarf