[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Conditional initialization and instantiation of reference type vars

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
JamesDykes
Asked:
JamesDykes
1 Solution
 
CEHJCommented:
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
 
tenderfootCommented:
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
 
wide_awakeCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
wide_awakeCommented:
Oops... duplicated CEHJ's answer.  Never mind :)
0
 
JamesDykesAuthor Commented:
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
 
wide_awakeCommented:
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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now