bonjour,
je souhaite créer à titre d'entraînement à la manipulation des fenêtres une application de moyennage. elle est composée d'une fenêtre principale, dans laquelle j'entre le nombre de valeurs à traiter, ce qui ouvre une deuxième fenêtre dans laquelle je reçois les valeurs en question.
voici mon code:
je fais donc un tableau de JLabel et un tableau de JTextField correspondant au nombre de valeurs à traiter transmis en argument pour l'instanciation de l'objet fen2=new Fen2;
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 package moy; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; class FenPale extends JFrame implements ActionListener { FenPale (){ setTitle ("Fenêtre principale"); System.out.println("constructeur FenPale"); setBounds (200,200,400,100); Container contenu=getContentPane(); contenu.setLayout(new GridLayout(2,2,10,10)); val =new JLabel ("nombre de valeurs"); nbval=new JTextField(5); moy=new JLabel("moyenne"); moyval=new JTextField(5); moyval.setEditable(false); contenu.add(val); contenu.add(nbval); contenu.add(moy); contenu.add(moyval); nbval.addActionListener(this); } public void actionPerformed (ActionEvent ev) { if (ev.getSource()==nbval) { System.out.println("évenement"); String nval=nbval.getText(); System.out.println("votre entrée : "+nval); nbrval=Integer.parseInt(nval); System.out.println("modif :"+nbrval); entreedonnees(nbrval); } } private void entreedonnees (int n) { System.out.println("valeur transmise: "+n); Fen2 fen2=new Fen2(n); fen2.setVisible (true); } private JLabel val, moy; private JTextField nbval, moyval; private String nval, moyenne; private int nbrval, moyen; } class Fen2 extends JFrame implements ActionListener { Fen2(int nb) { setTitle ("entrée des données"); System.out.println("donnée reçue :"+nb); setBounds (600,200,400,(nb*50)+50); Container contenu2=getContentPane(); contenu2.setLayout(new GridLayout(2,nb+1,10,10)); for (int i=1;i<nb+1;i++) { aff[i]=new JLabel("valeur "+i); val[i]=new JTextField(5); contenu2.add(aff[i]);contenu2.add(val[i]); } bouton=new JButton ("valider"); contenu2.add(bouton); bouton.addActionListener(this); } public void actionPerformed (ActionEvent e) { if (e.getSource()==bouton) { } // traitement des informations : construction objet tableau de valeurs } private JLabel aff[]; private JTextField val[]; private JButton bouton; } public class Moyenne { public static void main(String[] args) { System.out.println("main"); FenPale fen=new FenPale(); fen.setVisible(true); } }
problème, je bute sur ceci:
la ligne 63 correspondant àException in thread "AWT-EventQueue-0" java.lang.NullPointerException
at moy.Fen2.<init>(Moyenne.java:63)
at moy.FenPale.entreedonnees(Moyenne.java:43)
at moy.FenPale.actionPerformed(Moyenne.java:37)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
fen2 ne s'affiche pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part aff[i]=new JLabel("valeur "+i);
bien évidemment, lorsque je supprime la boucle if du constructeur de Fen2 cela marche très bien, mais je n'ai plus de TextFields pour rentrer mes valeurs, simplement le bouton.
j'ai beau chercher, je ne vois pas le souci, et en quoi j'ai une NullPointerException.
quelqu'un aurait-il la gentillesse de m'éclairer ?
Partager