Bonjour à tous;
Je suis débutant en JAVA ;
Mon probleme c'est que j'ai creer une interface simple avec un bouton et 2 zones de textes
le programme doit copier le texte dans le champ 1 vers le champ 2
et je tombe dans ce problème.
voila le code de la premiere classe
le code de deuxième classe
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 import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Fenetre extends JFrame{ private JTextField txt1; private JTextField txt2; public Fenetre() { super(); Build(); } public void Build(){ setTitle("Mon App"); setSize(200,200); setLocationRelativeTo(null); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix setContentPane(buildContentPane()); } private JPanel buildContentPane(){ JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); JButton bouton = new JButton(new CopierAction(this,"Copier")); panel.add(bouton); JTextField txt1 = new JTextField("aa"); txt1.setColumns(10); panel.add(txt1); JTextField txt2 = new JTextField(); txt2.setColumns(10); panel.add(txt2); return panel; } public String getTxt1(){ return txt1.getText(); } public void SetTxt2(JTextField txt){ txt2.setText(txt.getText()); } public static void main(String[] args) { Fenetre fenetre = new Fenetre(); fenetre.setVisible(true); } }
et voici le message d'erreur:
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 import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JTextField; public class CopierAction extends AbstractAction { private Fenetre fenetre; public CopierAction (Fenetre fenetre,String txt){ super(txt); this.fenetre=fenetre; } public void actionPerformed(ActionEvent arg0) { String txt = fenetre.getTxt1(); //javax.swing.JOptionPane.showMessageDialog(null,txt); fenetre.SetTxt2((JTextField) txt); } }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Fenetre.getTxt1(Fenetre.java:44)
at CopierAction.actionPerformed(CopierAction.java:21)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
Partager