0

So I use the line a lot.

FileReader reader = new FileReader(file)

and I want to make this line a member variable. Like this:

private FileReader reader = new FileReader(file);

The problem is that java doesn't like this and gives the error:

The default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor Java(16777362)

So I made a constructor like this:

protected Json() throws FileNotFoundException
{
    //
}

the problem is, is that I use the JSON class in different classes and those classes will give the same error, so I had to edit the other classes, but that ended up looking really ugly and gave a lot of problems. Is there a solution to this problem?

3
  • Did you consider to handle (try/catch) the exception sooner? Or is that not feasible? Commented Oct 20, 2020 at 8:47
  • Also take a look at this, this or this, all of which discuss the same problem Commented Oct 20, 2020 at 8:49
  • use an aggregation instead on a composition 👉🏻 stackoverflow.com/a/1468285/8030651 ➡️ you can localize problem with FileReader initialization Commented Oct 20, 2020 at 8:52

1 Answer 1

0

So I decided to do it like this

JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader(file));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.