0

I create login form and when press button, i want pass entered name to another class.

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         

    get_name = this.nameField.getText();
    get_pass = this.passwordField.getText();

}

In another class this prints null,

public static void main(String[] args) {
    LoginFrame logframe = new LoginFrame();
    System.out.println(logframe.get_name);
}
user2092457
  • 11
  • 1
  • 1

2 Answers2

1

The main() method gets called before get_name has a value set which is why you are getting null. If you debug the application (or just move the print) chances are get_name wont be null after okButtonActionPerformed() gets called.

Also its not a good practice to have public variables, you should set them to private and use getters and setters to access them instead. Here is a list of good reasons to do so.

Community
  • 1
  • 1
0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • It always print value before action perform. How can i get value after action performed? – user2092457 May 13 '13 at 21:11
  • move it some place where the value from the `textfield` has already been retrieved – 0x6C38 May 13 '13 at 21:13
  • I write 'public String getName(){return get_name;}' out of action performed and use this funtion in another class but it can't change – user2092457 May 13 '13 at 21:21
  • `getName()` is a method, you can't have a method inside a method in Java, I suggest you familiarize yourself with the [basics](http://www.oracle.com/technetwork/java/index-138747.html) before you continue further. – 0x6C38 May 13 '13 at 21:27
  • I try that but it prints frame0 – user2092457 May 13 '13 at 21:34
  • if you move the statement as it is ofc its not going to work... change it to `System.out.println(this.get_name);` but honestly I insist there is a lot you need to learn there is no point on me getting your code to work if you don't understand what you are doing – 0x6C38 May 13 '13 at 21:42
  • i don't know much thing about gui, sorry. It didn't work also, I think it can't be done in java, so i'll change my algorithm. – user2092457 May 13 '13 at 21:51
  • this has nothing to do with the gui this is about the very basics and LOL ofc it can be done in Java, this and things 1million times more complicated its not that hard just grab a book or some online guide and give it a read there is loads of places you can learn for free the problem here is code needs to make sense in order to work – 0x6C38 May 13 '13 at 21:55
0

The action listener takes it's action while running time, but static variables and methods will take their value(s) while compile time, so you can define your textfield(s) to some default values.
See this example:

private static int a ;
public static void test(){
 a = 3;
System.out.println(a);
a=5; 
System.out.println(a);
}

You probably guess the output will be (3 then 5), but actually it's not correct static variables will take the lase value, so the output will be

5
5

That's why they create dynamics as it's value(s) will be change at run time like your okButtonActionPerformed gets the values of textfields after compiling(at run time), but while compiling it's set to null so the output will be

null

You probably have a problem with accessing some classes field(s), see this example :

class Fruit{
 private String type;

 public String getType(){
       return this.type;
 }

 public void setType(String type){
       this.type = type;
}
}

public class Main{
//default constructor 
 public Main(){
 Fruitobj = new Fruit();
 obj.setType("Apple");
 System.out.println(obj.getType());
}
}

See also this tutorial, and this also a good article answered by StackOverFlow.

Community
  • 1
  • 1
Azad
  • 5,047
  • 20
  • 38