Reputation: 25
Question: Why is the String name
always null?
This is how I create the dialog Add
in my class:
public void init (){
try {
Add dialog = new Add();
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE );
//dialog.add(comp)
dialog.setModal(true) ;
//dialog.setModalityType(dialog.DEFAULT_MODALITY_TYPE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
Here I get the value from the user:
public void actionPerformed(ActionEvent arg0) {
name = textField.getText();
System.out.println(name);
setVisible(false);
}
The method to get user's value from another class:
public String Get(){
return name;
}
Here I try to use the value, but name
is always null
:
Add l = new Add();
l.init();
String name = l.Get();
Upvotes: 1
Views: 168
Reputation: 3409
Problem you created Add
instance two times. Remove Add dialog = new Add();
in init()
method, it will work.
public void init (){
try {
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE );
//dialog.add(comp)
this.setModal(true) ;
//dialog.setModalityType(dialog.DEFAULT_MODALITY_TYPE);
this.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1