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 java.lang.NullPointerException


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 66
    Points : 41
    Points
    41
    Par défaut problème de java.lang.NullPointerException
    J'ai une Fenetre comportant un JPanel (jPanelTrajet) et un bouton (jButtonAcq), un cliq sur ce bouton doit afficher un trait dans le jPanelTrajet, quand je clic sur le bouton j'ai l'erreur suivante:

    Exception in thread "Thread-2" java.lang.NullPointerException
    at essai.Trajet.traceTraj(Trajet.java:36)
    at essai.Trajet.manuel(Trajet.java:25)
    at essai.Trajet.run(Trajet.java:29)

    j'ai cru comprendre que cette erreur faisait suite a l'appel d'un objet pointant sur null, or j'ai beau chercher je ne vois pas l'erreur dans mon code.

    classe main:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class Main {
     
     
        public Main() {
        }
     
     
        public static void main(String[] args) {
            Application appli= new Application();
        }
     
    }
    classe InterfaceGraphique
    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
     
     
    package essai;
     
    import java.awt.Graphics;
     
     
    public class InterfaceGraphique extends javax.swing.JFrame {
        private Graphics g;
        private int hauteur=0;
        private int largeur=0;
        private Application app=null;
     
        public InterfaceGraphique() {
     
        }
     
          public InterfaceGraphique(Application app) {
            initComponents();
            this.setSize(1024,760);
            this.app=app;
            this.g=g;
            this.largeur=jPanelTrajet.getWidth();
            this.hauteur=jPanelTrajet.getHeight();
        }
     
          public Graphics getG(){
              return this.g;
          }
     
          public int getLargeur(){
              return this.largeur;
          }
     
          public int getHauteur(){
          return this.hauteur;
          }
     
         public void paint(Graphics g){
             super.paint(g);
         }
     
     
        private void initComponents() {
    //code généré par netbeans pour l'interface graphique que j'ai supprimé pour le forum
        }                    
     
        private void jButtonDemarrerAcqActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    repaint();
    app.demarrerApplication();
    // TODO add your handling code here:
        }                                                  
     
      /* 
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new InterfaceGraphique().setVisible(true);
                }
            });
        }*/
     
        private javax.swing.JButton jButtonDemarrerAcq;
        private javax.swing.JPanel jPanelTrajet;
     
     
    }
    classe Application:
    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
     
     
    package essai;
     
    import java.awt.Graphics;
     
     
     
     
    public class Application {
        private InterfaceGraphique interGraph=null;
        private Trajet t=null;
        private Graphics g;
     
     
     
        public Application() {
            this.interGraph=new InterfaceGraphique(this);
            this.interGraph.setVisible(true);
             this.t=new Trajet(this.interGraph.getG(),this.interGraph.getLargeur(),this.interGraph.getHauteur());
        }
     
        public void init(){
            initObjects();
        }
     
        public void initObjects(){
            this.t=new Trajet(this.interGraph.getG(),this.interGraph.getLargeur(),this.interGraph.getHauteur());
        }
     
        public void demarrerApplication(){
            this.interGraph.repaint();
            this.t.start();
        }
     
    }
    classe Trajet:
    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
     
     
    package essai;
     
    import java.awt.*;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    public class Trajet extends Thread{
        private Graphics g;
     
     
        public Trajet() {
        }
     
        public Trajet(Graphics g,int largeur,int hauteur){
        this.g=g;
     
        }
     
        public void manuel(){
            traceTraj(this.g);
        }
     
        public void run(){
            manuel();
        }
     
        private void traceTraj(Graphics g) {
            Graphics2D g2D=(Graphics2D)g;
            g2D.setPaint(Color.GREEN);
     
            this.g.drawLine(50,50,200,100);
        }
     
     
    }
    Merci d'avance à ceux qui se pencherons sur mon problème

  2. #2
    Membre éclairé
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Points : 704
    Points
    704
    Par défaut
    j'ai pas tester mais juste une question

    dans le constructeur de InterfaceGraphique ta ca
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public InterfaceGraphique(Application app) {
            initComponents();
            this.setSize(1024,760);
            this.app=app;
            this.g=g;
            this.largeur=jPanelTrajet.getWidth();
            this.hauteur=jPanelTrajet.getHeight();
        }
    tu fais jPanelTrajet. mais je vois pas dans cette classe ou tu fais un new JPanel ou quelque chose. c normal?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 66
    Points : 41
    Points
    41
    Par défaut
    C'est bon j'ai trouvé le problème, il fallait rajouter "this.g=jPanelTrajet.getGraphics();" dans le constructeur de la classe interface.

  4. #4
    Membre régulier
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 121
    Points
    121
    Par défaut
    Juste super() en debut de cette classe ne suffit pas ?

    Comme tu étend une classe je te le conseille fortement !!!!

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

Discussions similaires

  1. [EJB3.1] Injection avec @ejb et problème de java.lang.NullPointerException
    Par ensatTetouan dans le forum Java EE
    Réponses: 1
    Dernier message: 04/03/2012, 03h00
  2. Problème de "java.lang.NullPointerException" dans une JSP
    Par abdoulfall dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 11/02/2012, 04h46
  3. Problème avec "java.lang.NullPointerException"
    Par Yokooo dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 02/04/2010, 16h28
  4. Problème de java.lang.NullPointerException
    Par tpdm dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 02/07/2008, 13h54
  5. Problème de java.lang.NullPointerException
    Par anduril dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 27/05/2007, 17h07

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