Solved

Constructor

Posted on 2004-10-10
10
150 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java jdbc batch example 8 41
eclipse apache tomcat admin console 52 124
Error in @AspectJ Based AOP with Spring 2 18
Java syntax, or is it Selenium 6 30
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

820 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