0

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?

1
  • 1
    Because you've never created the instance of Login class! So it will be null. The solution is creating login instance, ex: Login login = new Login(); Commented May 21, 2012 at 2:49

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

would that mean it will perform all of its tasks again even thought I have finished with login and only require PortalHandler?
@user521180: that makes no sense. If you need a Login object, then you need a Login object. Period.
@user521180 If you post more of your code from ThirdClass I can give you a better idea of what to do.
Say i log in using the login class and i do not need it anymore. how ever i do need the instance of PortalHandler that was created by login. how do I get it?
@user52: If the only way to get a PortalHandler is through a Login object, then you need a valid reference to an instantiated Login object.
|
2

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.

Comments

2

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.

1 Comment

added a possible problem with the answers.. plz verify it!!
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.