aspose file tools*
The moose likes Java in General and the fly likes Instance attribute initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Instance attribute initialization" Watch "Instance attribute initialization" New topic
Author

Instance attribute initialization

maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

Hi all,
I have a question about instance variables initialization. Hi have the following code



My doubt is why, even though I'm initializing the 'var1' instance attribute at the declaration point, I get a null at the time the method 'printOutput' is run?
Initialization of type 'var1' is made through a static method ('getType' in the example) in another class from another package.
If I initialize the 'var1' attribute in the same 'printOutput' method just before printing it works fine.



Thanks a lot
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Then at the time you execute printOutput, the either var1 has been set to null after it was initialized, or var1.getName() returns null.

Does the MyObject constructor initialize whatever it needs to in order for getName() to return a value?

rc
maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

'var1' attribute is null at the time the code reaches the println method. So it would throw a NullPointerException
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

maximiliano

What you have just said implies the call to getType() is returning null, unless there is other code which could possibly change the value of var1.
maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

Well, in fact there is no other code that changes the value of the instance attribute. The attribute is declared and instantiated in one line and in a private method called from the constructor of the class a method of the 'var1' attribute is called. Is there any restriction or problem in that sequence?

Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Why don't you just show us the real code? Or at least a short version of the real code which actually exhibits the problem? Looking at a bogus code fragment where you assert various things about the code you didn't post is not very practical. Show us the code.
maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

Hi guys,
I'm sorry for leaving this thread unattended all these days but I was ill and with a lot of work afterwards.
Anyway I have solved the problem. It was all because of the way constructors were called. Constructors were not finished initialising when I intended to use an instance attribute still not initialised.
In the end it was java basics, but something I did not see at fisrt.
Here is the code so you can comment on it and give me your opinion.

Thanks in advance!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance attribute initialization