1

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..

5
  • Are you sure those values aren't null... that you are calling eu.setName(un, unType); with Commented Nov 17, 2015 at 16:24
  • Check what you are passing in here... eu.setName(un, unType); Commented Nov 17, 2015 at 16:25
  • Well if you are going to initialize a new instance, your user_name will be null as in, not assigned anything. Carry the instance through the session, request, or whatever the scope. Commented Nov 17, 2015 at 16:25
  • 3
    It looks like you're creating a new instance of Entities.Users eu = new Entities.Users() that doesn't have the values set Commented Nov 17, 2015 at 16:25
  • When I print values in my 'setName()' it shows the values.. But when it return Nothing... Commented Nov 17, 2015 at 16:27

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Let's assume like this..........I store username and user type from my login form through setName method..Then somewhere else in my application I want those username and user type... So what should I do for that without creating obj from Entities.Users class...!!
@JLink If you have additional questions you need to ask them in separate post. Also try to avoid XY problem in the future. Always explain what you are trying to achieve and then create simplified example. This answer explains problem from current version of your question so please consider accepting it (or other answer you prefer).
You should keep separate the reference of your different user object from each other. Please see the edited part as an example.
0

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());
    }
}

Comments

0

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());

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.