IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Java Discussion :

Un problème d'exécution: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException


Sujet :

Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Un problème d'exécution: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Bonjour,
    J'aimerais solliciter votre aide concernant mon problème.
    Voici l'erreur lors de l'exécution :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at Echiquier.paintComponent(Echiquier.java:67)
            at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
            at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
            at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedFPScales(RepaintManager.java:1707)
            at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1616)
            at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
            at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
            at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
            at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
            at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
            at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
            at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
            at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
            at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
            at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1890)
            at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
            at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
            at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
            at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
            at java.base/java.security.AccessController.doPrivileged(Native Method)
            at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
            at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
            at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
            at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
            at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
            at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
            at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
            at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Et voici le code du programme concerné :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
     
        public void paintComponent(Graphics g){
             int taille1=50;//taille de la case
             g.setColor(Color.BLACK);
             boolean bool = false;
             for (int i = 0; i < 8; i ++) {//ligne 8 cases
                  for (int y = 0; y < 8; y ++) {//colonne 8 cases
                    if(bool){
                    g.setColor(Color.WHITE);
                    g.fillRect((y+1)*taille1 , (i+1)*taille1, taille1, taille1);//(ligne,colonne,longueur,largeur)
                  }else{
                    g.setColor(Color.LIGHT_GRAY);
                    g.fillRect((y+1)*taille1 , (i+1)*taille1, taille1, taille1);
                  }
                  bool =! bool;
     
                  //INSERTION IMAGE SELON LA POSITION DES PIECES QUI SONT JOUER
                  if(this.plateau[i][y].getPiece().getNom()== "Pion"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/PB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/PN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
                  if(this.plateau[i][y].getPiece().getNom()== "Tour"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/TB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/TN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
                  if(this.plateau[i][y].getPiece().getNom()== "Cavalier"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/CB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/CN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
                  if(this.plateau[i][y].getPiece().getNom()== "Fou"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/FB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/FN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
                  if(this.plateau[i][y].getPiece().getNom()== "Reine"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/RRB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/RRN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
                  if(this.plateau[i][y].getPiece().getNom()== "Roi"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/RB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/RN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
     
                }//Fin boucle y
                bool =! bool;
              }//Fin boucle i
              g.setColor(Color.BLACK);
              for(int c = 0; c < 8 ; c ++){// CHIFFRE
                String le="";
                le+=c+1;
                g.drawString(le,40, (c+2)*taille1);
              }
     
              for(int c = 0; c < 8 ; c ++){// LETTRE
                char le = lettre[c];
                g.drawString(""+le,(c+1)*taille1, 40);
              }
            }
        //
    Quelques informations complémentaires:
    -getNom() retourne un String .
    -getPiece() retourne l'objet Piece.
    -Toutes les images sont dans le répertoire "img".
    -Lors de la compilation aucun problème n'a été détecté.
    Je voudrais remercier toutes les réponses qui pourraient m'aider à résoudre ce problème.

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    peux-tu nous montrer la ligne 67 du fichier Echiquier.java ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at Echiquier.paintComponent(Echiquier.java:67)
    Eric

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut,
    peux-tu nous montrer la ligne 67 du fichier Echiquier.java ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at Echiquier.paintComponent(Echiquier.java:67)
    Eric
    Oui bien sûre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(this.plateau[i][y].getPiece().getNom()== "Pion"){
    Et voici le paragraphe entier concernant cette ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if(this.plateau[i][y].getPiece().getNom()== "Pion"){
                    if(this.plateau[i][y].getPiece().getCouleur()=="Blanc"){
                      ImageIcon icon = new ImageIcon("img/PB.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }else{
                      ImageIcon icon = new ImageIcon("img/PN.png");
                      icon.paintIcon(null, g, (y+1)*taille1, (i+1)*taille1);
                    }
                  }
    Merci d'avance d'avoir apporté ton aide

  4. #4
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Salut,
    donc
    fais d´abord ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(  this.plateau[i][y] != null && this.plateau[i][y].getPiece() != null && this.plateau[i][y].getPiece().getNom()== "Pion"){
    Eric

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    Salut,
    donc
    fais d´abord ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(  this.plateau[i][y] != null && this.plateau[i][y].getPiece() != null && this.plateau[i][y].getPiece().getNom()== "Pion"){
    Eric
    Merci pour ton aide j'ai remplacé toutes les conditions et ça fonctionne, mais je ne comprends pas mon erreur pourrais-tu me l'expliquer ? ?
    Et aussi, peut-on réduire la condition comme ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     if (this.plateau[i][y].getPiece() != null && this.plateau[i][y].getPiece().getNom()== "Pion")
    Je pense que mon erreur c'est je n'ai pas mit "il faut que l'objet soit présent c'est à dire existante afin de pouvoir poser l'image" c'est bien ça ?
    Or si l'objet n'existe pas, le programme va dire qu'il y a une erreur non pas à la compilation mais à l'exécution

  6. #6
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    cela veut dire aussi que
    cet object n´existe pas (encore )
    ou bien cet Objet
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.plateau[i][y].getPiece()
    n´existe pas (encore)

    Eric

  7. #7
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2019
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par jeffray03 Voir le message
    salut,
    cela veut dire aussi que
    cet object n´existe pas (encore )
    ou bien cet Objet
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.plateau[i][y].getPiece()
    n´existe pas (encore)

    Eric
    Salut,
    Merci beaucoup ton aide je comprends bien maintenant.
    Mille mercis à toi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 08/02/2010, 08h23
  2. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ?
    Par freezerhm dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 04/12/2007, 09h26
  3. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par Trint dans le forum Interfaces Graphiques en Java
    Réponses: 6
    Dernier message: 27/02/2007, 11h28
  4. Réponses: 8
    Dernier message: 11/05/2006, 19h32
  5. [JDIC]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par kedare dans le forum Concurrence et multi-thread
    Réponses: 4
    Dernier message: 06/05/2006, 22h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo