Why a crash ?

Discussion in 'Java' started by Armel HERVE, Sep 29, 2003.

  1. Armel HERVE

    Armel HERVE Guest

    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>
    Armel HERVE, Sep 29, 2003
    #1
    1. Advertising

  2. Armel HERVE

    VisionSet Guest

    "Armel HERVE" <> wrote in message
    news:...
    > 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.

    --
    Mike W
    VisionSet, Sep 29, 2003
    #2
    1. Advertising

  3. Armel HERVE

    Armel HERVE Guest

    In article <4XVdb.2562$>,
    says...

    > 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>
    Armel HERVE, Sep 29, 2003
    #3
  4. Armel HERVE

    Stefan Poehn Guest

    "Armel HERVE" <> schrieb im Newsbeitrag
    news:...
    > 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
    Stefan Poehn, Sep 29, 2003
    #4
  5. Armel HERVE

    VisionSet Guest

    "Armel HERVE" <> wrote in message
    news:...
    > In article <4XVdb.2562$>,
    > says...
    >
    > > 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


    No you're right, there is nothing wrong with the code, it compiles and runs
    fine!

    --
    Mike W
    VisionSet, Sep 29, 2003
    #5
  6. Armel HERVE

    VisionSet Guest

    "Stefan Poehn" <> wrote in message
    news:bl9cso$vo0$...
    > "Armel HERVE" <> schrieb im Newsbeitrag
    > news:...
    > > 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 :-(
    --
    Mike W
    VisionSet, Sep 29, 2003
    #6
  7. Armel HERVE

    Armel HERVE Guest

    In article <bl9cso$vo0$>, says...
    > "Armel HERVE" <> schrieb im Newsbeitrag
    > news:...
    > > 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.
    >
    > > <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.
    >

    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.
    > Regards
    > Stefan
    >

    You're welcome
    >
    >
    Armel HERVE, Sep 29, 2003
    #7
  8. Armel HERVE

    Stefan Poehn Guest

    "VisionSet" <> schrieb im Newsbeitrag
    news:fJWdb.2596$...
    > "Stefan Poehn" <> wrote in message
    > news:bl9cso$vo0$...
    > > "Armel HERVE" <> schrieb im Newsbeitrag
    > > news:...

    > [...]
    > 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
    Stefan Poehn, Sep 29, 2003
    #8
  9. Armel HERVE

    Roedy Green Guest

    On Mon, 29 Sep 2003 15:42:50 +0200, "Stefan Poehn"
    <> wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 29, 2003
    #9
  10. Armel HERVE

    Stefan Poehn Guest

    "Roedy Green" <> schrieb im Newsbeitrag
    news:...
    > On Mon, 29 Sep 2003 15:42:50 +0200, "Stefan Poehn"
    > <> wrote or quoted :
    >
    > >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.


    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
    Stefan Poehn, Sep 30, 2003
    #10
  11. Armel HERVE

    Armel HERVE Guest

    In article <blb6ip$lap$>, says...
    > "Roedy Green" <> schrieb im Newsbeitrag
    > news:...
    > > On Mon, 29 Sep 2003 15:42:50 +0200, "Stefan Poehn"
    > > <> wrote or quoted :
    > >
    > > >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.

    >
    > 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 J2SE Engineering
    Sun Microsystems Java Software http://java.sun.com/j2se
    Armel HERVE, Sep 30, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach

    Re: Why does this crash?

    Alf P. Steinbach, Jul 18, 2003, in forum: C++
    Replies:
    5
    Views:
    363
  2. Developwebsites

    why does it crash?

    Developwebsites, Oct 10, 2003, in forum: C++
    Replies:
    6
    Views:
    352
    Jerry Coffin
    Oct 11, 2003
  3. ksharma
    Replies:
    5
    Views:
    409
    pembed2003
    May 5, 2004
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,765
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page