Why a crash ?

A

Armel HERVE

Hi everybody,

is there someone who can explain me why the following program crashes ?

exeption :
java.lang.NullPointerException
at com.test.TestNull$InnerClass.access$0(TestNull.java:11)
at com.test.TestNull$InnerClass$AbstractClassImpl.getLength
(TestNull.java:18)
at com.test.TestNull$AbstractClass.<init>(TestNull.java:25)
at com.test.TestNull$InnerClass$AbstractClassImpl.<init>
(TestNull.java:16)
at com.test.TestNull$InnerClass$AbstractClassImpl.<init>
(TestNull.java:16)
at com.test.TestNull$InnerClass.<init>(TestNull.java:13)
at com.test.TestNull.<init>(TestNull.java:8)
at com.test.TestNull.main(TestNull.java:32)
Exception in thread "main"

Thanks for your answers

Armel

<code>
package com.test;
class TestNull {
private String test = null;

public TestNull() {
test = "Hello";

InnerClass aa = new InnerClass();
}

private class InnerClass {
InnerClass() {
AbstractClassImpl zz = new AbstractClassImpl();
}

private class AbstractClassImpl extends AbstractClass {
int getLength() {
return TestNull.this.test.length();
}
}
}

private abstract class AbstractClass {
public AbstractClass() {
System.out.println("getLength()=" + getLength());
}

abstract int getLength();
}

public static void main(String[] args) {
TestNull testNull1 = new TestNull();
}
}
</code>
 
V

VisionSet

Armel HERVE said:
Hi everybody,

is there someone who can explain me why the following program crashes ?

exeption :
java.lang.NullPointerException

<code>
package com.test;
class TestNull {
private String test = null;

public TestNull() {
test = "Hello";

InnerClass aa = new InnerClass();
}

private class InnerClass {
InnerClass() {
AbstractClassImpl zz = new AbstractClassImpl();
}

private class AbstractClassImpl extends AbstractClass {
int getLength() {
return TestNull.this.test.length();
}
}
}

private abstract class AbstractClass {
public AbstractClass() {
System.out.println("getLength()=" + getLength());
}

abstract int getLength();
}

public static void main(String[] args) {
TestNull testNull1 = new TestNull();
}
}
</code>

Because AbstractClassImpl does not exist until the constructor completes,
and you are calling getLength() on it in the super class.
 
A

Armel HERVE

Because AbstractClassImpl does not exist until the constructor completes,
and you are calling getLength() on it in the super class.
I'm not sure it's the good answer : this code generate the same
execption :

java.lang.NullPointerException
at com.test.TestNull.access$0(TestNull.java:2)
at com.test.TestNull$InnerClass.getLength(TestNull.java:14)
at com.test.TestNull$AbstractClass.<init>(TestNull.java:20)
at com.test.TestNull$InnerClass.<init>(TestNull.java:11)
at com.test.TestNull$InnerClass.<init>(TestNull.java:11)
at com.test.TestNull.<init>(TestNull.java:8)
at com.test.TestNull.main(TestNull.java:29)
Exception in thread "main" in InnerClass::getLength

"in InnerClass::getLength" is displayed...
so the good method is called

<code>
package com.test;
class TestNull {
private String test = null;

public TestNull() {
test = "Hello";

InnerClass aa = new InnerClass();
}

private class InnerClass extends AbstractClass {
int getLength() {
System.out.println("in InnerClass::getLength");
return TestNull.this.test.length();
}
}

private class AbstractClass {
public AbstractClass() {
System.out.println("getLength()=" + getLength());
}

int getLength() {
return TestNull.this.test.length();
}
}

public static void main(String[] args) {
TestNull testNull1 = new TestNull();
}
}
</code>
 
S

Stefan Poehn

Armel HERVE said:
Hi everybody,

is there someone who can explain me why the following program crashes ?
[...]

Because you are making a design that nobody
can understand, see below...
It is not worth to look into this code why it crashes,
it is much better to get back to the design and start your
coding anew.
<code>
package com.test;
class TestNull {
private String test = null;

public TestNull() {
test = "Hello";

InnerClass aa = new InnerClass();
}

private class InnerClass {
InnerClass() {
AbstractClassImpl zz = new AbstractClassImpl();
}

private class AbstractClassImpl extends AbstractClass {
int getLength() {
return TestNull.this.test.length();

Even if you exactly know what TestNull.this
does, nobody else does.
You are making problems for others that read
your code and (obviously) for yourself.

Regards
Stefan
 
V

VisionSet

Stefan Poehn said:
Armel HERVE said:
Hi everybody,

is there someone who can explain me why the following program crashes ?
[...]

Because you are making a design that nobody
can understand, see below...
It is not worth to look into this code why it crashes,
it is much better to get back to the design and start your
coding anew.
<code>
package com.test;
class TestNull {
private String test = null;

public TestNull() {
test = "Hello";

InnerClass aa = new InnerClass();
}

private class InnerClass {
InnerClass() {
AbstractClassImpl zz = new AbstractClassImpl();
}

private class AbstractClassImpl extends AbstractClass {
int getLength() {
return TestNull.this.test.length();

Even if you exactly know what TestNull.this
does, nobody else does.
You are making problems for others that read
your code and (obviously) for yourself.

So you don't think he is just learning the language, rather than trying to
design a real system?
The code there is nothing more than a java programmer certification
question.
Which incidently I dropped a point on :-(
 
A

Armel HERVE

Armel HERVE said:
Hi everybody,

is there someone who can explain me why the following program crashes ?
[...]

Because you are making a design that nobody
can understand, see below...
It is not worth to look into this code why it crashes,
------------------------^ looking
So if it's easy to know why it's crash, why don't you say it to the
community ?
We need someone like you to make new guru!!!
it is much better to get back to the design and start your
coding anew.


Even if you exactly know what TestNull.this
does, nobody else does.
You are making problems for others that read
your code and (obviously) for yourself.
I have a problem and I need some support, not feel judged.
I'm not a newbie and if this code sounds hard to understand, it's
because it's only an excerpt.

For your information, TestNull.this means that in an inner class, I need
to access some fields of embedding class without any ambiguity.
 
S

Stefan Poehn

VisionSet said:
Stefan Poehn said:
[...]
So you don't think he is just learning the language, rather than trying to
design a real system?
In both cases I wanted to provide some help:
a) he is learning the language:
=> IMHO he should learn the language in such a
way that all his team members he will be working
with in the future can read his code
b) he is making a real system:
=> IMHO he should try to make a good design
to avoid big problems while coding
The code there is nothing more than a java programmer certification
question.

Are those certifications so heavy? I think I will never
get such a certification, provided I really want a
java problem generator certification, ooops, a java
programmer certification.

Regards
Stefan
 
R

Roedy Green

Because you are making a design that nobody
can understand, see below...
It is not worth to look into this code why it crashes,
it is much better to get back to the design and start your
coding anew.

To be helpful, you need to give some hints on just why it needs a
rewrite and what the improved code would look like.


For example, naming. If these are anything but experiments,
AbstractClass is a bad name because it tells you almost nothing about
what the class does.
 
S

Stefan Poehn

Roedy Green said:
To be helpful, you need to give some hints on just why it needs a
rewrite and what the improved code would look like.

It needs a rewrite because the author has a lot of knowledge
about java, but he cannot repair a 50LOC piece of software.
The criterion is as simple as that and I have used it very often
to see that I have to rewrite my code. Please have a look at
Martin Fowler's Refactoring book to see how easy refactoring
can be.

Stefan
 
A

Armel HERVE

It needs a rewrite because the author has a lot of knowledge
about java, but he cannot repair a 50LOC piece of software.
The criterion is as simple as that and I have used it very often
to see that I have to rewrite my code. Please have a look at
Martin Fowler's Refactoring book to see how easy refactoring
can be.

Stefan

THis is the answer from SUN

Hello

Thank you for reporting this issue.

This bug is being tracked under the following:

Bug Id: 4030374
Category: java
Subcategory: compiler
Synopsis: * Initialization of up-level links, immediately after
super(), occurs too late.

Here is the public summary from the bug report:


In previous releases of the compiler,
up-level references from a class's instance methods did not work until
after
the superclass constructor has returned. For example, the following
program
would fail at runtime:

class SuperInitBug {
static class S {
void hi() { System.out.println("You shouldn't see this
message"); }
S() { hi(); }
}
static class T {
void greet() { System.out.println("You won't see this
either."); }
class N extends S {
void hi() {
T.this.greet(); //throws a
NullPointerException!!
}
}
}
public static void main(String av[]) { new T().new N(); }
}

Fixing this bug required initializing the synthetic member
this$0 before calling the superclass constructor. Although
specifically allowed by the VM specification (4.8.2),
the verifier failed to allow it prior to 1.4. Therefore, this code
change only appears when you use the -target 1.4 compiler flag.
(end of public summary).

Please refer to the bug report for more information:
http://developer.java.sun.com/developer/bugParade/bugs/4030374.html


To gain the advantage of this fix, you will need to compile
your .java files with:
javac -target 1.4 [... other javac flags...] source.java

Hope this helps, and Best Regards-
Tim Bell (e-mail address removed) J2SE Engineering
Sun Microsystems Java Software http://java.sun.com/j2se
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top