2015-11-08 49 views
0

好吧我一直在做這個作業幾個小時了。 對不起,如果英文不好,我剛剛從谷歌翻譯。我們將有一個非常特殊的大猩猩,我們將有一個區域(20 x 20陣列),最初我們將有一個區域(20 x 20陣列)。 。將創建5男5只女大猩猩那麼我們必須把在隨機位置的所有大猩猩會在陣列移動,不能有超過三個大猩猩在一個空間對於這些大猩猩的共存必須遵循下列規則:。同步線程上的NullPointerException

如果在一個單元格中發現兩隻大猩猩,他們是同性兩個大猩猩的一人死亡(這應該是隨機進行)如果兩個大猩猩有不同的性別,一個新出生(性別,還必須選擇隨機),他們應該繼續各自的方式,我們必須確定sh是並同步所有的線程。「

而且我這樣做: 主營:

public static void main(String[] args) { 
    InformacionGorila info1 = new InformacionGorila(true, 1); 
    InformacionGorila info2 = new InformacionGorila(true, 2); 
    Gorilas gorila1 = new Gorilas(info1); 
    Gorilas gorila2 = new Gorilas(info2);  
    gorila1.start(); 
    gorila2.start(); 
} 

InformacionGorila類:

public class InformacionGorila { 

private boolean genero; 
private String sexo; 
private int id; 

public InformacionGorila(boolean genero, int id) { 
    this.genero = genero; 
    this.id = id; 
    DecisionDeGenero(genero); 
} 

private void DecisionDeGenero(boolean genero) { 
    if (genero == true) { 
     setSexo("Macho"); 
    } else if (genero == false) { 
     setSexo("Hembra"); 
    } 
} 

public int getId() { 
    return id; 
} 

public boolean getGenero() { 
    return genero; 
} 

public void setGenero(boolean genero) { 
    this.genero = genero; 
} 

public String getSexo() { 
    return sexo; 
} 

private void setSexo(String sexo) { 
    this.sexo = sexo; 
} } 

Gorilas類:

public class Gorilas extends Thread { 

private final InformacionGorila info; 
private Territorio terr; 

public Gorilas(InformacionGorila info) { 
    this.info = info; 
} 

public InformacionGorila getInfo() { 
    return info; 
} 

@Override 
public void run() { 
    try { 
     terr.Convivencia(info); 
     System.out.println("Gorila: " + info.getId() + " Genero: " + info.getSexo()); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} } 

而且Territorio類:

public class Territorio { 

public boolean ocupado = false; 
public int cont = 0; 
Gorilas[][] gorilas = new Gorilas[20][20]; 

public synchronized void Convivencia(InformacionGorila info) { 
    while (ocupado) { 
     try { 
      wait(); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(Territorio.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
    cont++; 
    if (cont == 2) { 
     ocupado = true; 
    } 

    jaula(info); 
} 
int j = 0; 
int cont2 = 0; 

private synchronized void jaula(InformacionGorila info) { 
    String g = null; 
    String h = null; 
    while (cont2 != 2) { 
     if (j == 0) { 
      g = info.getSexo(); 
      j++; 
     } else if (j == 1) { 
      h = info.getSexo(); 
     } 
     cont2++; 
    } 
    decision(g, h); 

} 

public void decision(String g, String h) { 
    if (g.equals(h)) { 
     InformacionGorila info = new InformacionGorila(true, 11); 
     Gorilas gorila = new Gorilas(info); 
     gorila.start(); 
    } 
} } 

當然還沒有結束,但是當我嘗試測試它,我得到空指針異常的程序來調用terr.Convivencia(info);在Gorilas類方法運行時。有人能告訴我爲什麼嗎?哪裏不對?或者我做錯了什麼?

這些是例外:

異常在線程 「線程0」 異常在線程 「線程1」>顯示java.lang.NullPointerException 在hilossync.Gorilas.run(Gorilas.java:28 ) 顯示java.lang.NullPointerException 在hilossync.Gorilas.run(Gorilas.java:28)

+0

的[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is可能的複製-a-null-pointer-exception-and-how-do-i-fix-it) –

回答

1

你有宣佈 Territorio TERR,但不初始化它,使它一個空對象。

應該讀起來更像

Territorio terr = new Territorio(); 
+0

哦!是啊!謝謝你,先生!沒看見!哈哈 –