Solved

Constructor

Posted on 2004-10-10
10
154 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 30 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

624 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