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:
classe InterfaceGraphique
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 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
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 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 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(); } }
Merci d'avance à ceux qui se pencherons sur mon problème
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); } }
Partager