Home arrow static arrow Java Programming [Archive] - Can enclosing instance for an inner class be null?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Can enclosing instance for an inner class be null?
This topic has 10 replies on 1 page.

Posts:9
Registered: 7/30/04
Can enclosing instance for an inner class be null?  
Aug 3, 2004 3:17 PM



 
I have a class Solution, which defines an inner class, SolutionStep.

In a third class I construct several SolutionStep objects, and later use them to construct a Solution.

In this third class I have statements like this:

Solution solution = null;
. . .
aCollection.add(solution.new SolutionStep(arg1, arg2);

This has been working fine, but recently a customer reported an error,which seems to be a NullPointerException at the line above.

Now that I look at this code, I'm not very happy about it and I'll clean it up, but I am left with the basic question of whether what I'm doing is legal. Can the enclosing instance used to construct an inner class be null? I have not been able to find a definitive answer.

Any help appreciated.
 

Posts:2,830
Registered: 9/1/03
Re: Can enclosing instance for an inner class be null?  
Aug 3, 2004 3:19 PM (reply 1 of 10)



 
is solutionStep static ? if it is, then i would be inclined to say "yes".

if it isn't, then no.
 

Posts:9
Registered: 7/30/04
Re: Can enclosing instance for an inner class be null?  
Aug 3, 2004 3:46 PM (reply 2 of 10)



 
No, solutionStep is not static. But SolutionStep is a simple class that serves only as a temporary container for a couple of values, and it never makes any reference to its enclosing class or its members.

And, as I say, this has been working fine for months. I have single-stepped through the code today, and I can see that the enclosing instance is null, but it doesn't seem to matter.

Clearly there's no reason for this to be an inner class, in the first place, but I'm left unsure about whether this is what's causing my customer's problem.
 

Posts:27,518
Registered: 11/3/97
Re: Can enclosing instance for an inner class be null?  
Aug 3, 2004 4:25 PM (reply 3 of 10)



 
I don't see how the above code could ever be legal.

First the class is not fully qualified which would be a requirement to use it in a "third" class.

Second it is missing a paren and I don't see any place to put it that would make the code more legal.

Hopefully someone will point out what I am missing.
 

Posts:9
Registered: 7/30/04
Re: Can enclosing instance for an inner class be null?  
Aug 3, 2004 4:43 PM (reply 4 of 10)



 
Yes, you're right, I dropped a parenthesis. Sorry.

The offending statement is actually

aCollection.add(solution.new SolutionStep(arg1, arg2));

And that is certainly legal. The inner class does not need to be qualified when it's constructed in the context of an enclosing instance. You can see this from the compiler message that results from this statement:

aCollection.add(new Solution.SolutionStep(arg1, arg2));

The compiler message is:

"No enclosing instance of type Solution is accessible. Must qualify the allocation with an enclosing instance of type Solution (e.g. x.new A() where x is an instance of Solution). "

In this case, I think "x" corresponds to solution, and "A" is SolutionStep.

In debug mode after the aCollection.add(solution.new SolutionStep(arg1, arg2)); statement successfully executes, I see that aCollection contains a Solution$SolutionStep object with two members (arg0 and arg1) and this:

this$0 = null

So the inner class instance has been constructed, but the reference to its enclosing class is null, which is not surprising.
 

Posts:9
Registered: 7/30/04
Re: Can enclosing instance for inner class be null? The Spec.  
Aug 3, 2004 4:50 PM (reply 5 of 10)



 
I don't often venture into the deep waters of the Java Language spec, but I found this, which seems relevant:

<excerpt>
15.9.4 Run-Time Evaluation of Class Instance Creation Expressions

At run time, evaluation of a class instance creation expression is as follows.

First, if the class instance creation expression is a qualified class instance creation expression, the qualifying primary expression is evaluated. If the qualifying expression evaluates to null, a NullPointerException is raised, and the class instance creation expression completes abruptly. If the qualifying expression completes abruptly, the class instance creation expression completes abruptly for the same reason.
</excerpt>

Looks like I've been getting away with an illegality for some time (and continue to do so, as do others who use my software).

The customer with the problem is using j2sdk1.4.2_05, as am I.

Now I wonder how it is that he has a problem when I don't.

Hmmm.
 

Posts:6,750
Registered: 1/25/04
Re: Can enclosing instance for inner class be null? The Spec.  
Aug 4, 2004 8:25 AM (reply 6 of 10)



 
Different platforms, or the same?
 

Posts:27,518
Registered: 11/3/97
Re: Can enclosing instance for an inner class be null?  
Aug 4, 2004 9:06 AM (reply 7 of 10)



 
Yes, you're right, I dropped a parenthesis. Sorry.

The offending statement is actually

aCollection.add(solution.new SolutionStep(arg1,
arg2));

And that is certainly legal. The inner class does not
need to be qualified when it's constructed in the
context of an enclosing instance.

Very interesting.

The following code demonstrates this....

    class Solution    {        class SolutionStep        {            public SolutionStep()            {            }         }    }     public class ThirdClass    {        static public void main(String[] args)        {        Solution solution = null;        //Solution solution = new Solution();  // This produces no null exception.         Solution.SolutionStep s = solution.new SolutionStep();        }    } 


Using jikes and javac doesn't change the behavior so that means it is VM rather than compiler specific.

I am using 1.4.2_04 on windows and I get the null pointer exception.

Looking at the javap output suggests that invokespecial has to be checking this (although I could have overlooked something when I checked.)

This probably comes from the following in the VM spec under invokespecial...


Otherwise, if objectref is null, the invokespecial instruction throws a NullPointerException.
 

Posts:9
Registered: 7/30/04
Enclosing class null: Compiler or VM?  
Aug 4, 2004 3:40 PM (reply 8 of 10)



 
My customer is running the same O/S platform as I am (Windows XP professional).

I sent him the executable and the source code.

He can run the executable I sent him just fine, but, when he recompiles it , the resulting executable gives the NullPointerException.

This leads me to think that our compilers must generate different code, but we both seem to be using j2sdk 1.4.2_05-b04.

Obviously one of my facts is wrong.
 

Posts:27,518
Registered: 11/3/97
Re: Enclosing class null: Compiler or VM?  
Aug 5, 2004 8:06 AM (reply 9 of 10)



 
My customer is running the same O/S platform as I am
(Windows XP professional).

I sent him the executable and the source code.

He can run the executable I sent him just fine, but,
when he recompiles it , the resulting executable gives
the NullPointerException.

This leads me to think that our compilers must
generate different code, but we both seem to be using
j2sdk 1.4.2_05-b04.

Obviously one of my facts is wrong.

You might want to verify that both of you are using the same compiler and VM. Windows comes with a VM and by default it is in the path.
 

Posts:9
Registered: 7/30/04
Re: Enclosing class null: Compiler or VM?  
Aug 6, 2004 6:03 PM (reply 10 of 10)



 
You might want to verify that both of you are using the same compiler and VM. Windows comes with a VM and by default it is in the path.

You're right. The time zone difference between me and Switzerland , and other circumstances, make prolonged discussions unattractive. I changed the code to avoid the null enclosing class and that solved the problem, so I'm chalking up yet another one to experience.

This has been quite good for me, since it made me think a bit more about inner classes, and a couple of things I've never quite understood but just always managed to make work.

And I've never monitored this forum before, but I'll be keeping my eye on it now.

Thanks for the help.
 
This topic has 10 replies on 1 page.