Solved

Constructor

Posted on 2004-10-10
10
144 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

13 Experts available now in Live!

Get 1:1 Help Now