I'm confused about initializing the Swing components like JTextField and other components. Calling from the DoSth class, the getTxtUser returns null pointer exception whereas if getTxtUser() from its own class returns the value correctly.Please help me.Thanks Code:
public class GUI{
private JLabel lblUname;
private JTextField txtUname;
public void showGUI(){
lblUname = new JLabel("Username");
txtUname = new JTextField(20);
....................
}
public String getTxtUser(){
return this.txtUname.getText();
}
}
public class DoSth(){
..............
GUI g = new GUI();
String user = g.getTxtUser(); //null pointer Exception even if it has some value
...............
}