• Post Reply Bookmark Topic Watch Topic
  • New Topic

IOException in constructor but not if externally called.

 
Bolimous Prime
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem where when a create an new filelist object I want it to populate using a the constructor. The problem is that I get an IOException error if I use the contructor this way. I can call it externally without problems (i.e. fl.populatelist() .
Code below
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two solutions:
1. Give your constructor a throws clause.

2. Do not call populateList() in the constructor.
Personally, I would recommend the latter. It is usually "not the done thing" for constructors to throw exceptions.
Rather than calling populateList() in your constructor you could either get the caller to call it specifically after constructing your object, or call it behind-the-scenes the first time your object's getter method is called. This is called lazy construction (lazy, in this case = good).
You might also want to make the filename a class-level property rather than hard-coding it in your populateList() method. Perhaps your constructor could accept a String or File parameter for the filename.
 
Bolimous Prime
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I wont have the filename hardcoded, I will be doing it the way you suggested after I get the core mechanics worked out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!