Bonjour,
je vous soumets un problème que je rencontre depuis ce matin et dont
je ne vois vraiment pas comment m'en sortir...
Jé développe une petite appli pour gérer des ligues de sports...
Tout d'abord, voici l'erreur qui est généré.
Remarque : on a aucune indication sur la ligne qui plante !
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sun.java.swing.plaf.windows.WindowsProgressBarUI.paintDeterminate(Unknown Source)
at javax.swing.plaf.basic.BasicProgressBarUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
J'ai créé une interface qui comporte notamment un JProgressBar pour indiquer à l'utilisateur l'évolution du traitement et un bouton pour démarrer le processus...
Le bouton contient le code suivant : «
for (i=0; i < modele.getListeSports().size(); i++) {
Sports s = (Sports) modele.getListeSports().get(i);
myListLiguesToMAJ.clear();
for (int k=0; k < s.getListeLigues().size(); k++) {
Ligues L = (Ligues)s.getListeLigues().get(k);
myListLiguesToMAJ.add(L);
}
pasIncrementation = ((myListLiguesToMAJ.size() / cptLigueToMaj)*100);
if (s.getNomSport().equals("Football")) {
System.out.println("appel au thread");
// On créé le thread...
Thread t = new Thread(tg,
new Traitement.MajLigueFootball(
modele,
myListLiguesToMAJ,
myBarreProgression,
pasIncrementation
),
"numero "+(i+1));
t.start();
} else if (s.getNomSport().equals("Rugby")) {
// Lance le thread pour le rygby...
} else if (s.getNomSport().equals("Volley")) {
// Pour chaque sport, on lance le bon thread qui doit
// effectuer le traitement
}
}
// Processus qui permet d'attendre la fin des autres
// pour rendre les éléments de l'interface à nouveau disponible
// à l'utilisateur...
new ThreadFinProcessus(tg).start(); »
Dans les attributs de mon JPanel, j'ai rajouté un ThreadGroup : « private ThreadGroup tg = new ThreadGroup("groupe"); »
et j'ai développé une innerClass pour détecter la fin des divers processus et rendre la main à l'utilisateur :
/**
* Thread permettant de rendre la main à l'utilisateur
* une fois que tous les thread ont été terminés...
*/
private class ThreadFinProcessus extends Thread {
private ThreadGroup tg;
public ThreadFinProcessus(ThreadGroup t) {
tg = t;
}
public void run() {
// Attends que les threads se terminent...
while (tg.activeCount() > 0) {
Thread.yield();
}
myBarreProgression.setValue(100);
try {
Thread.sleep( 1000 );
} catch (Exception e) { }
myBarreProgression.setValue(0);
myBarreProgression.updateUI();
debloquerInterfaceGraphique();
}
}
Le traitement fonctionne bien une fois, deux fois, trois fois et au bout d'un certain nombre de fois (c'est très variable), l'erreur décrite ci-dessus se lève. Avez-vous une idée ????
Merci pour votre aide.
Fabien