简体   繁体   中英

Getter always return Null Value

Well this is my code

package Entities;

public class Users {

     String user_name;
     String user_type;

    public void setName(String un, String type) {
        user_name = un;
        user_type = type;
    }
    public String getName(){

        return user_name;
    }
    public String getType(){
        return user_type;
    }

}

Actually this a basic thing and sorry for asking you guys..In my login interface I set username and user Type

Entities.Users eu = new Entities.Users();
eu.setName(un, unType);

Then somewhere else in home page I want this username and user type and I call getter like this

Entities.Users eu = new Entities.Users();
System.out.println(eu.getName());
System.out.println(eu.getType());

But it always return NULL !!!!!! Why is that..

The problem is you are creating new object of user class before printing:

Entities.Users eu = new Entities.Users();
System.out.println(eu.getName());
System.out.println(eu.getType());

This makes a new reference of eu , so the attributes of eu ,

String user_name;
String user_type;

are NULL .

You should do the following:

Entities.Users eu = new Entities.Users();
eu.setName(un, unType);
System.out.println(eu.getName());
System.out.println(eu.getType());

But make sure un , and unType are not NULL .


EDIT: You can do something like the following:

Entities.Users user_1 = new Entities.Users();
Entities.Users user_2 = new Entities.Users();
user_1.setName("user_1", "user_type_1");
user_2.setName("user_2", "user_type_2");
System.out.println(user_1.getName()); // user_1
System.out.println(user_2.getType()); // user_type_2

What's happening is that you are creating a new instance of the Entities.Users class, and are properly setting the names. However, when you are trying to access it later in the class, you're creating a new instance that doesn't have any knowledge of the first. Try something like this

public class MyClass {
    private Entities.Users eu = new Entities.Users();

    private void myMethod() {
        //not sure where these two params come from
        eu.setName(un, unType);
    }

    private void myGetMethod() {
        System.out.println(eu.getName());
        System.out.println(eu.getType());
    }
}

Your code should be like this ..

Entities.Users eu = new Entities.Users();
eu.setName(un, unType);
//where un and unType should not be null
//because you did not expecting null 
//Example:
//eu.setName("Some Name", "Some Type");
System.out.println(eu.getName());
System.out.println(eu.getType());

This is because you are creating a new instance of Entities.Users. Entities.Users eu = new Entities.Users(); creates a new instance and this object will be different from the other Users objects.

So if you want to use the same object, you need to either pass the one created to the second place. Or persist it somewhere (db, file etc) and call the persisted object from the second place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM