[JAVA]

Fermé
Julie35 Messages postés 2 Date d'inscription mercredi 21 mars 2007 Statut Membre Dernière intervention 27 mars 2007 - 27 mars 2007 à 20:47
 Utilisateur anonyme - 30 mars 2007 à 10:04
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:
public class Main {
    
  
    public Main() {
    }
    

    public static void main(String[] args) {
        Application appli= new Application();
    }
    
}


classe InterfaceGraphique
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:
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:
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 ;)
A voir également:

1 réponse

Utilisateur anonyme
30 mars 2007 à 10:04
Salut!

Pour moi, ta classe Application ne sert à rien ici. Je l'ai donc supprimée.

De plus, je ne comprends pas pourquoi ta classe Trajet est un Thread. J'ai donc enlevé cela aussi.

J'ai transformé ta classe Trajet en JPanel que j'ai ajouté sur ta JFrame InterfaceGraphique.

J'ai enlevé toute les variables d'instance de type Graphics, car chaque composant graphique connaît un objet Graphics. Il suffit d'appeler la méthode getGraphics() pour le récupérer.

Ton bouton n'était jamais initialisé. C'est pour cela que tu recevais un NullPointerException. Je l'ai initialisé et je l'ai déplacé dans la classe Trajet.

Dans la classe trajet, dans le constructeur qui prend largeur et hauteur en arguments, j'ai ajouté ceci pour fixer la taille de ton JPanel.
this.setPreferredSize(new Dimension(largeur, hauteur));


J'ai modifié ta classe Main. C'est elle qui démarre maintenant ton application.

Tu trouveras le code adapté ci-dessous. A toi de faire le reste ;-)

Classe Main:
package essai;

public class Main {
	public static void main(String[] args) {
		InterfaceGraphique view = new InterfaceGraphique();
		view.pack();
		view.setVisible(true);
	}
}


Classe Trajet
package essai;

import java.awt.*;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Trajet extends JPanel {
	private JButton jButtonDemarrerAcq;

	public Trajet() {
		super();
		initObjects();
	}

	public Trajet(int largeur, int hauteur) {
		this();
		this.setPreferredSize(new Dimension(largeur, hauteur));
	}

	private void initObjects() {
		jButtonDemarrerAcq = new JButton("Dessiner une ligne");
		jButtonDemarrerAcq.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				drawLine(getGraphics());
			}
		});
		add(jButtonDemarrerAcq);

	}
	public void drawLine(Graphics g) {
		Graphics2D g2D = (Graphics2D) g;
		g2D.setPaint(Color.GREEN);
		g.drawLine(50, 50, 200, 100);
	}
}


Classe InterfaceGraphique:
package essai;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class InterfaceGraphique extends JFrame {
	private int hauteur = 0;

	private int largeur = 0;

	private JPanel jPanelTrajet;

	public InterfaceGraphique() {
		super();
		initComponents();
		this.largeur = jPanelTrajet.getWidth();
		this.hauteur = jPanelTrajet.getHeight();
	}

	public int getLargeur() {
		return this.largeur;
	}

	public int getHauteur() {
		return this.hauteur;
	}

	private void initComponents() {
		jPanelTrajet = new Trajet(320,200);
		getContentPane().add(jPanelTrajet);
	}
}



HackTrack
;-)
0