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

Langage Java Discussion :

erreur de java.lang.NullPointerException


Sujet :

Langage 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 erreur de java.lang.NullPointerException
    J'ai une erreur à la compilation de mon code :

    Exception in thread "main" java.lang.NullPointerException
    at javaapplication7.Application.init(Application.java:21)
    at javaapplication7.Application.<init>(Application.java:14)
    at javaapplication7.Main.main(Main.java:26)
    Java Result: 1

    En me renseignant j'ai vu que cette erreur est dûe à une tentative d'accès à un objet non instancié, or dans mon cas je ne vois pas ce que je n'ai pas instancier.

    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();
     
        }
     
    }
    la 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
    import java.io.File;
    import javax.swing.JFileChooser;
     
     
    public class Application {
        private Imag i;
        private InterfaceGraphique interGraph=null;
     
     
        public Application() {
     
            this.interGraph=new InterfaceGraphique(this);
            this.interGraph.setVisible(true);
            init();
     
        }
     
        public void init(){
     
            this.i=new Imag(interGraph.getG());
        }
     
        public void DemarrerAcquisition(){
            i.start();
        }
     
    }
    la 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
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class InterfaceGraphique extends javax.swing.JFrame {
        private Graphics g;
        private Application app=null;
     
     
     
     
        public InterfaceGraphique() {
     
        }
     
        public InterfaceGraphique(Application app) {
            initComponents();
            this.app=app;
            this.g=jPanelTrajet.getGraphics();
     
        }
     
        public void paint(Graphics g){
            super.paint(g);
        }
     
        public Graphics getG(){
            return this.g;
        }
     
     
        private void initComponents() {
    //ici se trouve le code généré par netbeans que j ai effacé car trop long et inutile pour le forum
    }                     
     
        private void jButtonOnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    repaint();
        }                                         
     
        public JPanel getPanelFichier(){
            return this.jPanelFichier;
        }
     
     
        public JPanel getPanelVisualisation(){
            return this.jPanelVisualisation;
        }
    la classe Image:
    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
     
    import java.awt.*;
    import java.io.File;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class Imag extends Thread {
     
        private Graphics g;
     
     
        public Imag() {
     
        }
     
        public Imag(Graphics g){
            this.g=g;
     
        }

    l'erreur se situe dans la classe Application, quand j'appelle le constructeur de la clase Imag : this.i=new Imag(interGraph.getG());


    Merci d'avance pour votre aide

  2. #2
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Si je ne me tompe pas quand tu fait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    this.i=new Imag(interGraph.getG());
    tu appelle la methode getG qui doit retourner un objet Graphic qui n'a pas encore été créé. C'est ça qui pose problème.

Discussions similaires

  1. Réponses: 9
    Dernier message: 31/05/2011, 10h40
  2. erreur compilation java.lang.NullPointerException
    Par muslim1987 dans le forum JDBC
    Réponses: 6
    Dernier message: 26/06/2008, 09h16
  3. Erreur exception java.lang.NullPointerException
    Par geol99 dans le forum Langage
    Réponses: 2
    Dernier message: 14/06/2007, 20h24
  4. [Débutant] Erreur java.lang.NullPointerException
    Par Kevin12 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/02/2007, 15h48
  5. Probleme erreur java.lang.NullPointerException
    Par Tsukaasa dans le forum Langage
    Réponses: 4
    Dernier message: 25/05/2006, 18h19

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