File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance attribute initialization

 
maximiliano marasso
Greenhorn
Posts: 10
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'var1' attribute is null at the time the code reaches the println method. So it would throw a NullPointerException
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic