• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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.
 
They weren't very bright, but they were very, very big. Ad contrast:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic