Não estou conseguindo fazer meus Threads funcionarem, alguém poderia me explicar que tem de errado ???
ERRO:
Exception in thread “Thread-1” Exception in thread “Thread-5” Exception in thread “Thread-2” Exception in thread “Thread-3” Exception in thread “Thread-4” java.lang.NullPointerException
at filosofogui.FilosofoGUI.Repinta(FilosofoGUI.java:307)
at filosofogui.FilosofoGUI.SetInfo(FilosofoGUI.java:300)
at filosofogui.Filosofo.setStatus(FilosofoGUI.java:338)
at filosofogui.Filosofo.run(FilosofoGUI.java:368)
CÓDIGO:
[code]public void SetInfo(int key, int status) {
if (key == 0) {
if (status == 0) {
Repinta(jPanelFilosofo0, Color.yellow, Label1,“Socrates - Pensando”);
} else if (status == 1) {
Repinta(jPanelFilosofo0, Color.green, Label1,“Socrates - Comendo”);
} else if (status == 2) {
Repinta(jPanelFilosofo0, Color.red, Label1,“Socrates - Faminto”);
}
} else if (key == 1) {
if (status == 0) {
Repinta(jPanelFilosofo1, Color.yellow, Label2,“Platão - Pensando”);
} else if (status == 1) {
Repinta(jPanelFilosofo1, Color.green, Label2,“Platão - Comendo”);
} else if (status == 2) {
Repinta(jPanelFilosofo1, Color.red, Label2, “Platão - Faminto”);
}
} else if (key == 2) {
if (status == 0) {
Repinta(jPanelFilosofo2, Color.yellow, Label3,“Aristóteles - Pensando”);
} else if (status == 1) {
Repinta(jPanelFilosofo2, Color.green, Label3,“Aristóteles - Comendo”);
} else if (status == 2) {
Repinta(jPanelFilosofo2, Color.red, Label3,“Aristóteles - Faminto”);
}
} else if (key == 3) {
if (status == 0) {
Repinta(jPanelFilosofo3, Color.yellow, Label4,“Pitagoras - Pensando”);
} else if (status == 1) {
Repinta(jPanelFilosofo3, Color.green, Label4,“Pitagoras - Comendo”);
} else if (status == 2) {
Repinta(jPanelFilosofo3, Color.red, Label4,“Pitagoras - Faminto”);
}
} else if (key == 4) {
if (status == 0) {
Repinta(jPanelFilosofo4, Color.yellow, Label5,“Epicuro - Pensando”);
} else if (status == 1) {
Repinta(jPanelFilosofo4, Color.green, Label5,“Epicuro - Comendo”);
} else if (status == 2) {
Repinta(jPanelFilosofo4, Color.red, Label5, “Epicuro - Faminto”);
}
}
Repinta(jContentPane, Color.white, jlbMesa, null); //at filosofogui.FilosofoGUI.SetInfo(FilosofoGUI.java:300)
}
public void Repinta(JPanel p, Color c, JLabel l, String s) {
p.setBackground©;
l.setText(s); //at filosofogui.FilosofoGUI.Repinta(FilosofoGUI.java:307)
}
}// Fim Da classe Frame
//*****************************************
// cada filósofo é visto como um processo
class Filosofo extends Thread {
// identifica o filósofo
private int key;
// 0 = pensa , 1 = come , 2 = faminto
@SuppressWarnings(“unused”)
private int status;
// objeto jantar
private FilosofoGUI j;
public Filosofo(int key, FilosofoGUI j) {
this.key = key;
this.j = j;
}
// pega identificador do filósofo
public int getKey() {
return key;
}
// seta status do filósofo
public void setStatus(int i) {
status = i;
switch (i) {
case 0:
j.SetInfo(key, 0); //at filosofogui.Filosofo.setStatus(FilosofoGUI.java:338)
break;
case 1:
j.SetInfo(key, 1);
break;
case 2:
j.SetInfo(key, 2);
break;
}
}
// no estado pensando bloqueia por um certo tempo 2000ms)
private void pensando() {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
// no estado comendo bloqueia por um certo tempo (2500 ms)
private void comendo() {
try {
Thread.sleep(2500);
} catch (Exception e) {
}
}
@Override
public void run() {
while (true) { // roda indefinidamente
setStatus(0); // filósofo pensando //at filosofogui.Filosofo.run(FilosofoGUI.java:368)
pensando(); // pensa - bloqueia por certo tempo
j.fork.pick2(this); // filósofo pega os garfos
comendo(); // come - bloqueia por certo tempo
j.fork.put2(this); // libera os garfos }
}
}
}
[/code]