Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

IOException in constructor but not if externally called.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic