Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why a crash ?

Reply
Thread Tools

Why a crash ?

 
 
Armel HERVE
Guest
Posts: n/a
 
      09-29-2003
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.jav a:11)
at com.test.TestNull$InnerClass$AbstractClassImpl.get Length
(TestNull.java:1
at com.test.TestNull$AbstractClass.<init>(TestNull.ja va:25)
at com.test.TestNull$InnerClass$AbstractClassImpl.<in it>
(TestNull.java:16)
at com.test.TestNull$InnerClass$AbstractClassImpl.<in it>
(TestNull.java:16)
at com.test.TestNull$InnerClass.<init>(TestNull.java: 13)
at com.test.TestNull.<init>(TestNull.java:
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>
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      09-29-2003
"Armel HERVE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Armel HERVE
Guest
Posts: n/a
 
      09-29-2003
In article <4XVdb.2562$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.ja va:14)
at com.test.TestNull$AbstractClass.<init>(TestNull.ja va: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:
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>
 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      09-29-2003
"Armel HERVE" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      09-29-2003

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


 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      09-29-2003
"Stefan Poehn" <(E-Mail Removed)> wrote in message
news:bl9cso$vo0$(E-Mail Removed)...
> "Armel HERVE" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > 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


 
Reply With Quote
 
Armel HERVE
Guest
Posts: n/a
 
      09-29-2003
In article <bl9cso$vo0$(E-Mail Removed)>, (E-Mail Removed) says...
> "Armel HERVE" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > 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
>
>

 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      09-29-2003
"VisionSet" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fJWdb.2596$(E-Mail Removed)...
> "Stefan Poehn" <(E-Mail Removed)> wrote in message
> news:bl9cso$vo0$(E-Mail Removed)...
> > "Armel HERVE" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed)...

> [...]
> 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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2003
On Mon, 29 Sep 2003 15:42:50 +0200, "Stefan Poehn"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Stefan Poehn
Guest
Posts: n/a
 
      09-30-2003
"Roedy Green" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Mon, 29 Sep 2003 15:42:50 +0200, "Stefan Poehn"
> <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
CRASH - DirectX End-User runtime - CRASH - What to to ? reply@newsgroup.please Computer Support 1 01-05-2004 02:55 PM
why does it crash? Developwebsites C++ 6 10-11-2003 02:37 AM
Re: Why does this crash? Alf P. Steinbach C++ 5 07-18-2003 03:44 PM



Advertisments