?
Solved

Conditional initialization and instantiation of reference type vars

Posted on 2003-03-21
6
Medium Priority
?
374 Views
Last Modified: 2010-03-31
I would like to use a single variable name for multiple data (reference) types. I keep ending up with a "null pointer exception" at runtime, although the code compiles just fine. Here is the code in question:

//class fields
//won't compile without this line
private static Entity myEntity; //Entity is superclass of Job, Company, and Contact

//conditions to initialize and instantiate object variable
char entity = chooseEntity();
if (entity == 'j' | entity == 'J') {
     Job myEntity = new Job();
} else if (entity == 'C' | entity == 'c') {
     Company myEntity = new Company();
} else if (entity == 't' | entity == 'T') {
     Contact myEntity = new Contact();
}//end switch(entity)

//use object here
myEntity.inputFileName();


0
Comment
Question by:JamesDykes
[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
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8182152
You haven't actually reproduced the failing code. You probably need something like:

Entity myEntity = null;
char entity = chooseEntity();
if (entity == 'j' | entity == 'J') {
    myEntity = new Job();
} else if (entity == 'C' | entity == 'c') {
    myEntity = new Company();
} else if (entity == 't' | entity == 'T') {
    myEntity = new Contact();
}//end switch(entity)

0
 
LVL 1

Expert Comment

by:tenderfoot
ID: 8182826
You are getting the runtime null pointer exception because your static member 'myEntity' has not been assigned and is default initialised to null.

There is a concept called shadowing (Java Language Specification 6.3.1) in java which partly explains what is occurring in your application. In the example you've provided, you are creating another instance of myEntity within the 'if' clause rather than assigning the existing static member.

As CEHJ suggested you should directly assign myEntity to the objects being created.  

 
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8185317
private static Entity myEntity; //Entity is superclass of Job, Company, and Contact

//conditions to initialize and instantiate object variable
char entity = chooseEntity();
if (entity == 'j' | entity == 'J') {
    myEntity = new Job(); // do not declare a local var called "myEntity".  Just assign to the static var.
} else if (entity == 'C' | entity == 'c') {
    myEntity = new Company();
} else if (entity == 't' | entity == 'T') {
    myEntity = new Contact();
}//end switch(entity)

//use object here
myEntity.inputFileName();

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:wide_awake
ID: 8185318
Oops... duplicated CEHJ's answer.  Never mind :)
0
 

Author Comment

by:JamesDykes
ID: 8186370
So, does this mean that there is an automatic cast from a supertype to a subtype when you set supertype equal to a subtype, or is something else going on?
0
 
LVL 3

Accepted Solution

by:
wide_awake earned 300 total points
ID: 8186644
A variable can hold an instance of its own type, or any subclass.

Also, a variable that is of an interface type can hold any object that implements that interface.

The reason for this is that all subclasses (and interface implementers) have at least the same set of public methods and fields as the superclass (or interface).  Thus, the JVM can know for certain that any method that exists in the superclass will also exist in the subclass.  The JVM then delegates the method call to the actual object that exists at runtime (i.e. the method of the subclass, not the superclass).

For example, if you try to extend a class and make one of the superclass methods private, you will get a compile error.  If you were allowed to do this, it would break the JVM's ability to treat subclass objects as superclass objects.

In order to reference methods that don't exist in the superclass, you have to cast the variable explicitly to the subtype.



Hope that helps,

-Mark.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 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