简体   繁体   English

如何使用由其他类创建的类的实例?

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

Login.java Login.java

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

PortalHandler.java 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 ThirdClass.java我想获取由login.java创建的实例中的名称,但随后给出了空指针错误

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

How can it be done? 如何做呢?

You need to initialize login like so: Login login = new Login() . 您需要像这样初始化loginLogin login = new Login()

That's assuming of course that Login has a constructor that takes no arguments. 当然,这是假定Login具有一个不带参数的构造函数。


Edit in response to OP's comment 根据OP的评论进行编辑

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 . 然后,您可以使用portalHandler进行任何操作,而portalHandler login But you have to initialize login with new Login() (or one of Login 's constructors) before you can use it. 但是您必须先使用new Login() (或Login的构造函数之一new Login()初始化login ,然后才能使用它。

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. 在我这方面有很大的猜测,但是我想知道您的问题是否是您有多个Login变量,一个不为null并显示,另一个未显示为null,并且您试图调用方法。 You need a reference to the one that was displayed. 您需要参考所显示的内容。

Answer this: are you already showing a Login object? 回答这个问题:您已经在显示一个Login对象了吗?

If so, then you need a reference to that Login object in order to call the getPortalHandler() method. 如果是这样,则需要引用 Login对象才能调用getPortalHandler()方法。

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 . 这不会创建实例,而只是声明一个能够容纳Login类型的对象的变量。 When you write this line, it is just a pointer to a null , until you call either of these... 当您编写此行时,它只是一个指向null的指针,直到您调用其中任何一个...

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(); 这将创建该类的新实例,然后使您可以访问其中的方法,例如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. 如果需要在完成Login对象后保留PortalHandler以便使用,则需要在清理Login对象之前获得对PortalHandler的引用。

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. 在此示例中,Login实例仅在doLogin()方法的长度内存在,此后不再是有效的对象。 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. 但是,您在完成操作之前已经引用了PortalHandler对象,ans PortalHandler对象存储为全局变量,因此您可以随时使用PortalHandler

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. 伙计们,我认为问题可能在于user521180已经在其代码的其他位置创建了一个login实例,从而进一步创建了PortalHandler实例,他现在想从该PortalHandler对象访问name

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 . 您的解决方案使他要做的是创建另一个login对象,并通过这个新创建的对象访问name ,该name 可能具有完全不同的name因为它将是由另一个PortalHandler创建的不同name String。

@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 . @ user521180:如果是这种情况,那么您还需要在该代码中有一个方法(或全局变量),该方法创建一个login对象,该对象以某种方式返回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). 因为无法检索对引用丢失或不可访问的堆中对象的引用(有些人可能无法同意其不可能的部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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