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

AWT/Swing Java Discussion :

probléme de Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException


Sujet :

AWT/Swing Java

  1. #1
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut probléme de Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    bonjour
    j'ai essaye de faire une interface mais j'ai un probleme
    le compilateur il m 'affiche


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    voici une partie de code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public  void button2_clic(ActionEvent e)
    {
      width=image.getWidth();
    float height=image.getHeight(),
     
                    rows=height,
                    cols=width;
     
                 textarea1.setText(""+width);
     
    -----------------
    --------------
       }
    je sais que le probleme dans textarea pourtant j ai déclaré dans le constructeur de la classe

    svp aide moi

  2. #2
    Expert éminent sénior

    Avatar de snake264
    Homme Profil pro
    Datascientist chez Leboncoin
    Inscrit en
    Novembre 2006
    Messages
    2 914
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Datascientist chez Leboncoin
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2006
    Messages : 2 914
    Points : 13 312
    Points
    13 312
    Par défaut
    Cette exception veut tout simplement dire que tu essaie d'accéder à un objet qui a pour valeur "null". Regarde dans ton code si tu il y a pas un objet qui peut avoir cette valeur, genre typiquement un oublie d'allocation mémoire.

  3. #3
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.File;
    import java.awt.*;
    import java.awt.event.*;
    import javax.imageio.ImageIO;
     
    public class ihm extends JFrame
    {  BufferedImage image;
      float inergie;
      float width;
     JButton button1;
     JButton button2;
     JButton button3;
     JButton button4;
     String file;
     JLabel label1;
     JLabel label2;
     JLabel label3;
     JLabel label4;
     JLabel label5;
     JLabel label6;
     JLabel label7;
     JLabel label8;
     JLabel label9;
     JLabel label10;
     JLabel label11;
     JLabel label12;
     JLabel label13;
     JLabel label14;
     JLabel label15;
     JLabel label16;
     JTextArea textarea1;
     Container conteneur=null;
     JTextArea textarea2;
     JTextArea textarea3;
     JTextArea textarea4;
     JTextArea textarea5;
     JTextArea textarea6;
     JTextArea textarea7;
     JTextArea textarea8;
     JTextArea textarea9;
     JTextArea textarea10;
     JTextArea textarea11;
     JTextArea textarea12;
     JTextArea textarea13;
     JTextArea tt;
    public ihm()
    {
    super();
    this.setSize(900,900);
    this.setTitle("Indexation et recherche d'image par contenu");
    conteneur=this.getContentPane();
    conteneur.setLayout(null);
    button1=new JButton("Afficher l'image");
    button2=new JButton("traitemment");
    button3=new JButton("enregister dans la base de données");
    button4=new JButton("afficher les résultat ");
    button3.setBounds(350,350,150,20);
    button1.setBounds(10,350,150,20);
    button2.setBounds(400,350,150,20);
    button4.setBounds(550,350,150,20);
    conteneur.add(button4);
    conteneur.add(button2);
    conteneur.add(button1);
    conteneur.add(button3);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea tt=new JTextArea();
    JLabel label1=new JLabel();
    JLabel label2=new JLabel();
    JLabel label3=new JLabel();
    JLabel label4=new JLabel();
    JLabel label5=new JLabel();
    JLabel label6=new JLabel();
    JLabel label7=new JLabel();
    JLabel label8=new JLabel();
    JLabel label9=new JLabel();
    JLabel label10=new JLabel();
    JLabel label11=new JLabel();
    JLabel label12=new JLabel();
    JLabel label13=new JLabel();
    JLabel label14=new JLabel();
    JLabel label15=new JLabel();
    JLabel label16=new JLabel();
    JLabel label17=new JLabel();
    JLabel label18=new JLabel();
    JTextArea textarea1=new JTextArea("0");
    JTextArea textarea2=new JTextArea();
    JTextArea textarea3=new JTextArea();
    JTextArea textarea4=new JTextArea();
    JTextArea textarea5=new JTextArea();
    JTextArea textarea6=new JTextArea();
    JTextArea textarea7=new JTextArea();
    JTextArea textarea8=new JTextArea();
    JTextArea textarea9=new JTextArea();
    JTextArea textarea10=new JTextArea();
    JTextArea textarea11=new JTextArea();
    JTextArea textarea12=new JTextArea();
    JTextArea textarea13=new JTextArea();
    label1.setText("Extraire des informations sur la couleur:");
    label1.setBounds(20,50,400,20);
    conteneur.add(label1);
    textarea1.setBounds(150,90,100,20);
    conteneur.add(textarea1);
    Font font=new Font("Arial", Font.BOLD, 20);
    Font font1=new Font("Arial", Font.BOLD, 15);
    label1.setFont(font);
    label1.setForeground(Color.blue);
    label2.setText("la taille de l'image :");
    label2.setFont(font1);
    label2.setBounds(12,90,400,20);
    label3.setText("la somme des couleurs:");
    label3.setFont(font1);
    label3.setBounds(12,129,400,20);
    label4.setText("  rouge:");
    textarea2.setBounds(270,129,100,20);
    conteneur.add(textarea2);
    label4.setFont(font1);
    label4.setBounds(200,129,400,20);
    label5.setText(" blue:");
    textarea3.setBounds(550,129,100,20);
    conteneur.add(textarea3);
    label5.setFont(font1);
    label5.setBounds(500,129,400,20);
    label6.setText("verte:");
    label6.setFont(font1);
    label6.setBounds(700,129,400,20);
    textarea4.setBounds(750,129,100,20);
    conteneur.add(textarea4);
    conteneur.add(label2);
    conteneur.add(label3);
    conteneur.add(label4);
    conteneur.add(label5);
    conteneur.add(label6);
    label7.setText("La moyenne des couleurs:");
    label7.setFont(font1);
    label7.setBounds(12,180,400,20);
     
    label8.setText(" rouge:");
    label8.setFont(font1);
    label8.setBounds(200,180,400,20);
    textarea6.setBounds(260,180,100,20);
    conteneur.add(textarea6);
    label9.setText("blue:");
    label9.setFont(font1);
    textarea5.setBounds(550,180,100,20);
    conteneur.add(textarea5);
    label9.setBounds(500,180,400,20);
    label10.setText("verte:");
    label10.setFont(font1);
    label10.setBounds(700,180,400,20);
    textarea7.setBounds(750,180,100,20);
    conteneur.add(textarea7);
    conteneur.add(label7);
    conteneur.add(label8);
    conteneur.add(label9);
    conteneur.add(label10);
    label11.setText("La moyenne des couleurs:");
    label11.setFont(font1);
    label11.setBounds(12,230,400,20);
    label12.setText(" rouge:");
    textarea8.setBounds(260,230,100,20);
    conteneur.add(textarea8);
    label12.setFont(font1);
    label12.setBounds(200,230,400,20);
    label13.setText(" blue:");
    label13.setFont(font1);
    textarea9.setBounds(550,230,100,20);
    conteneur.add(textarea9);
    label13.setBounds(500,230,400,20);
    label14.setText("verte:");
    label14.setFont(font1);
    textarea10.setBounds(750,230,100,20);
    conteneur.add(textarea10);
    label14.setBounds(700,230,400,20);
    conteneur.add(label11);
    conteneur.add(label12);
    conteneur.add(label13);
    conteneur.add(label14);
    label15.setText("La Extraire des informations sur la texture :");
    label15.setForeground(Color.blue);
    label15.setFont(font);
    label15.setBounds(12,270,500,20);
    label16.setText(" Inertie:");
    label16.setFont(font1);
    label16.setBounds(200,310,400,20);
    textarea11.setBounds(260,310,100,20);
    conteneur.add(textarea11);
    label17.setText("Energie:");
    label17.setFont(font1);
    label17.setBounds(500,310,400,20);
    label18.setText("Enropie:");
    textarea12.setBounds(570,310,100,20);
    conteneur.add(textarea12);
    label18.setFont(font1);
    label18.setBounds(700,310,400,20);
    textarea13.setBounds(765,310,100,20);
    conteneur.add(textarea13);
    conteneur.add(label15);
    conteneur.add(label16);
    conteneur.add(label17);
     
    conteneur.add(label18);
     
     
    button1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent evt){
    button1_clic(evt);
    }
    }
    );
    button2.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent evt){
    button2_clic(evt);
    }
    }
    );
    button3.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent evt){
    button3_clic(evt);
    }
    }
    );
    button4.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent evt){
    button4_clic(evt);
    }
    }
    );
    show();
    }
    public void button1_clic(ActionEvent e) {
    Graphics g=null;
     file =imagefile.getImageFile();
        if (file != null) {
     
         try{
     image=ImageIO.read(new File(file));
      repaint();
        } catch(IOException ee)
            {
                ee.printStackTrace();
            } 
     
     
        }
    }
    public void  paint (Graphics g)
    {
     g.drawImage(image,14, 400,600,600, this);
    }
    public void button3_clic(ActionEvent e)
    {
    //textarea1.append(""+inergie);
    }
    public void button4_clic(ActionEvent e)
    {}
    public  void button2_clic(ActionEvent e)
    {
      width=image.getWidth();
    float height=image.getHeight(),
     
                    rows=height,
                    cols=width;
                   JTextArea textarea1=new JTextArea();
     
                 this. textarea1.setText(""+width);
    }
    }



    voici le code je vois pas ou l objet qui pointe sur null

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 25
    Points : 40
    Points
    40
    Par défaut
    J'ai un truc comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ihm.button2_clic(ihm.java:260)
    	at ihm$2.actionPerformed(ihm.java:217)
    ce qui pointe vers la première ligne de button2_clic. Je dirai donc que c'est image qui pose problème.

  5. #5
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 804
    Points
    48 804
    Par défaut
    votre image est à null.

  6. #6
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    l image n est pas null parce que quant je fais System.out.println(width)
    elle m 'affiche la taille de l image ou est le probleme donc ?
    merci de votre intérêt

  7. #7
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 804
    Points
    48 804
    Par défaut
    Citation Envoyé par Kymils Voir le message
    ce qui pointe vers la première ligne de button2_clic.
    Donc image est null.

  8. #8
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    j ai pas compris pourquoi null

  9. #9
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 804
    Points
    48 804
    Par défaut
    parce que vous ne lui avez pas attribué de valeur. Soit vous n'avez pas appelé, au vu de votre code button1_clic avant, soit il a déclenché une exception et n'a pas initialisé la valeur.

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 25
    Points : 40
    Points
    40
    Par défaut
    Citation Envoyé par tchize_ Voir le message
    Donc image est null.
    Oui, c'est bien ce que je disais ^^

    Citation Envoyé par mirafrance Voir le message
    j ai pas compris pour koi null
    On se base sur le code que tu nous as donné, or si j'ai bien compris il faudrait d'abord cliquer sur le bouton 1 pour charger l'image (c'est le seul endroit) mais pour cette partie là il nous manque imagefile qui n'est défini nul part... est-ce que tu as un code plus complet ?

  11. #11
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    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
     
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ihm.button2_clic(ihm.java:275)
    	at ihm$2.actionPerformed(ihm.java:218)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6263)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6028)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4630)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4460)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4460)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    voici l 'erreur

  12. #12
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    j'ai ajouté la 1ère instruction à la public void button2_clic(ActionEvent e)
    dans mon code
    le code est compilé sans erreur mais il m'affiche rien a textarea pourquoi pourtant j ai fais textearea1.append(""+width);

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 25
    Points : 40
    Points
    40
    Par défaut
    Je viens de voir un autre soucis: ton textarea1 est-il un attribut de la classe ihm ? Tout le laisse croire, donc remplaces

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    JTextArea textarea1=new JTextArea();
    par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    textarea1=new JTextArea();
    ton this.textarea1.setText(""+width) devrait alors marcher (penses à enlever l'espace)


    Bon courage

  14. #14
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    ça y est, j 'ai résolu mon problème, c'est un simple problème.
    J'ai été déclaré les composants deux fois:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    JTextArea textarea1;
    JTextArea textarea1=new JTextArea();
    la solution:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    JTextArea textarea1;
    textarea1=new JTextArea();

    merci quand même

  15. #15
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 25
    Points : 40
    Points
    40
    Par défaut
    C'est pas ce que je te disais y a une semaine, mais en oubliant les parenthèses au passage ?

  16. #16
    Futur Membre du Club
    Inscrit en
    Décembre 2010
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Décembre 2010
    Messages : 12
    Points : 8
    Points
    8
    Par défaut
    Citation Envoyé par mirafrance Voir le message
    oui j ai oublié les parenthéses merci kymils

+ 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