简体   繁体   中英

How can I use an instance of a class that was created by a different class?

Login.java

PortalHandler portalHandler = new PortalHandler(dataString);
...
public PortalHandler getPortalHandler(){
        return portalHandler;
    }

PortalHandler.java

public String getName()
{
return name;
}

ThirdClass.java I want to get to get the name in that instance created by login.java but following gives an null pointer error

Login login;
PortalHandler portalHandler = login.getPortalHandler();

How can it be done?

You need to initialize login like so: Login login = new Login() .

That's assuming of course that Login has a constructor that takes no arguments.


Edit in response to OP's comment

I think you're looking to do something like this:

Login login = new Login();
login.doLoginStuff();
PortalHandler portalHandler = login.getPortalHandler();
// Do nothing with login from here on out

You can then do whatever you need to do with portalHandler and forget about login . But you have to initialize login with new Login() (or one of Login 's constructors) before you can use it.

Big guess on my part, but I wonder if your problem is that you have more than one Login variable, one that isn't null and that displays, and one that wasn't shown, is null, and that you're trying to call a method on. You need a reference to the one that was displayed.

Answer this: are you already showing a Login object?

If so, then you need a reference to that Login object in order to call the getPortalHandler() method.

Your answer is within your question - "How can I use an instance of a class that was created by a different class?"

In order to use the instance, you first have to create it. You have the following line...

Login login;

This doesn't create an instance, it just declares a variable as being capable of holding an object of type Login . When you write this line, it is just a pointer to a null , until you call either of these...

login = new Login();
Login login = new Login();

This will create a new instance of the class, which then allows you to access methods in it, such as login.getPortalHandler();

If you need to retain the PortalHandler for use after you're finished with the Login object, you'll need to get a reference to PortalHandler before the Login object is cleaned up.

For example...

PortalHandler portalHandler;

public void doLogin(){
    Login login = new Login();
    login.performLogin();
    portalHandler = login.getPortalHandler();
}

In this example, the Login instance only exists for the length of the doLogin() method, after which it is no longer a valid object. However, you have taken a reference to the PortalHandler object before you're finished, ans have stored it as a global variable, so you'll be able to continue using the PortalHandler whenever you want.

Guys i think the problem could be that user521180 has already created an instance of login somewhere else in his code which has further created an instance of PortalHandler and he now wants to access the name from that PortalHandler object.

What your solutions are making him do is create an another login object and access name via this newly created object which may have a completely different value in name because it would be a different name String created by a different PortalHandler .

@user521180: if this is the case, then you need to also have a method (or a global variable) in that code, which creates a login object, which somehow returns the instance of PortalHandler . Because retrieving the reference to an object in the heap whose reference is lost or in-accessible is impossible (some might not agree on the impossible part of it).

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