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
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);
}
}
le code de deuxième classe
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);
}
}
et voici le message d'erreur:
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)
peut-etre devrais tu appeler la méthode buildContentPane() dans ta classe Fenetre
Recuperer un texte d'un JTextField
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.