Article View: borland.public.jbuilder.java.language
Article #16605Re: Problems with references to objects
From: Joe Grace
Date: Sat, 30 Jan 1999 00:00
Date: Sat, 30 Jan 1999 00:00
52 lines
2548 bytes
2548 bytes
Vladimir Olenin wrote:
> After construction of MainClass is competed i can fire an action. But
> then i get null pointer exception in inner class someAdapter of B class
> when i try to use subA class to getSomeObject.
> When i checked this out, it turned out that subA in B class is null
> indeed while subA in A class is not null.
>
Vladimir,
they key to understanding what's happening is in your comment, "// !!! NOTE
that subA is constructed outside A constructor", or rather 'Note that a is
NOT fully initialized until a.init(...) is called, and therefore subA is not
initialized until a.init(...) is called.'
So, according to your MainClass, the order of operations you're getting is
a constructs, including subA gets initialized to null (default value for
class member reference value)
b constructs, copies (the current) subA from a to initialize its b.subA
to null.
[Now, both a.subA and b.subA are null.]
THEN (once all the above MainClass initializations are done)
MainClass() gets called resulting in a.subA getting properly initialized
(albeit too late for b.subA, above).
[Now, a.subA is 'new A()', but b.subA is still null since it's never
been reinitialized after its construction.]
b.subA is still null... including when your ActionListener gets called
resulting in a NullPointerException..
To fix this problem, you need to fully initialize a.subA at a's
construction, or you need to construct b after a.init(). One way or
another, you need to ensure a.subA is fully initialized before b copies
a.subA for itself (i.e., into b.subA).
I hope this explanation helps.
Good luck,
= Joe =
> class MainClass{
> A a = new A();
> B b = new B(a);
>
> MainClass(){
> a.init(b);
> ...
> }
>
Technical Details
Message-ID:
<36B402A7.4D40F3E4@serv.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36B3A1CB.63576B20@collegemail.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,e19e76b5315896fb,start X-Google-Attributes: gid111d45,public X-Google-Thread: f74b3,e19e76b5315896fb,start X-Google-Attributes: gidf74b3,public From: Joe Grace <occam@serv.net> Subject: Re: Problems with references to objects Date: 1999/01/30 Message-ID: <36B402A7.4D40F3E4@serv.net> X-Deja-AN: 438945991 Content-Transfer-Encoding: 7bit References: <36B3A1CB.63576B20@collegemail.com> To: Vladimir Olenin <keenonjava@collegemail.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language,borland.public.jbuilder.java.swing