0

My current problem is that I would like to write to and read from a file, however, I keep trying to throw exceptions and instantiating my variables only to keep getting errors about how the variables I've declared 'could not have been instantiated.' I'm unsure how to fix this problem.

I've tried using PrintWriter and FileWriter, briefly tried BufferedWriter and other solutions to no avail. I do not know what else I can try.

{ public SettingsHandler() { File configFile=new File(this.getClass().getResource("file").getFile()); try{ file = new Scanner(configFile); }catch (FileNotFoundException e){ System.out.println("Config.ini not found"); } } public void saveSetting(String setting, String value) { FileWriter fw; try{ fw = new FileWriter("myfile.txt", true); }catch (IOException e){ } BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); } } 

Every time I try creating the PrintWriter, it gives me an error for the bw parameter: "variable fw might not have been initialized."

Does anyone know how to solve this issue?

3
  • You are supposed to set fw explicitly to null.If the IDE (compiler?) finds a path were the variable is not set, it will complain. In your case the catch block. Commented Jun 6, 2019 at 13:14
  • Or move the last 2 lines into the try block. Commented Jun 6, 2019 at 13:15
  • 1
    What do you expect it to do when it was unable to open the file due to some IOException, yet you ignore it and try to write to it anyway. Just let the IOException get handled by the caller to saveSetting Commented Jun 6, 2019 at 13:16

2 Answers 2

1

"variable fw might not have been initialized."

You need to see more closely your code. The IDE saw this scenario.

 FileWriter fw; try{ fw = new FileWriter("myfile.txt", true); ==> An exception can happen }catch (IOException e){ nothing to do... } BufferedWriter bw = new BufferedWriter(fw); ==> fw is not initialized.. PrintWriter out = new PrintWriter(bw); 

Workarounds for this...

Scenario 1

 FileWriter fw = null; // Very pointles... try{ fw = new FileWriter("myfile.txt", true); }catch (IOException e){ } BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); 

Scenario 2 Move to the try catch

 try{ FileWriter fw = new FileWriter("myfile.txt", true); //Well a little better BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); }catch (IOException e){ } 

And so on...

Sign up to request clarification or add additional context in comments.

2 Comments

Or maybe just do not ignore exceptions
Oh my gosh, I should have thought of this... thank you so much for your help! It is fixed
0

The error "variable fw might not have been initialized" will get resolved by simply initializing your variable fw to null!

FileWriter fw = null; is correct.

--Thanks for asking.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.