Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Constructor

Posted on 2004-10-10
10
Medium Priority
?
156 Views
Last Modified: 2010-03-31
Can somebody tell me whats wrong with the following class?

--------------------------------------------------------------
public class Account
 {
       private Key myKey;
       private String username;
       private String password;
       
       public Account(Chat serverRef, String user, String pass)
        {
           myKey = new Key(serverRef);
           username = user;
           password = pass;
        }
       
       public Account(Chat serverRef)
        {
              myKey = null;
              username = null;
              password = null;
        }
       
       public String getUsername()
        {
              return username;
        }
   
    public String getPassword()
     {
           return password;
     }
   
    public Key getKey()
     {
           return myKey;
     }
   
 
     
 }
--------------------------------------------------------

The problem is, I do smth like:

Account myAccount = new Account(aChatServerObject, myUsername, myPassword); // it is fine but not until I do:

Key temp = myAccount.getKey(); // then it returns java.lang.nullpointerException. Why is that??? I cant see whats wrong with that though~~~ Somebody?



0
Comment
Question by:jtcy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
10 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 12270217
Make sure you are not calling the
   public Account(Chat serverRef)
constructor.  That constructor does NOT initialize the key field (though it probabyl should).
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 120 total points
ID: 12271467
In other words: when you use the short (overloaded) constructor, you should be prepared that:
1) .getKey() returns a null value, or,
2) .getKey() is smart enough to return a substitute value when null, or,
3) the shorter constructor is smart enough to set the internal field 'myKey' to a useful value.

The chosen default value depends on the rest of the use of this class.

;JOOP!
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12273323
I guess Account is not getting instantiated......

even if .getKey() returns null value all that happens is temp becomes null.......

Can you call any other methods in Account  like .getUserName().....

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12274260
Nonsense! If  the constructor returns it carries a reference to an object!

So, Account is instantiated. You might verify this by:
    System.out.println("Instance = " + myAccount.toString());
printing the object address.
;JOOP!
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12291294
He gets such an exception only if myAccount is pointing to null at the time of calling getKey method on myAccount
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12298580
Right!

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12542796
I believe my answer was right and verbose enough for A

<*>
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question