Solved

Constructor

Posted on 2004-10-10
10
148 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 54
Groovy:unable to resolve class error 2 66
github account with ecipse 1 42
Arrays.asList  VS  ArrayList 4 43
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now