• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedWriter - writing a variable  RSS feed

 
Jhon Calado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
How are you?

I've started programming a few days ago so my post might be a little basic.

I'm trying to develop an application that writes to a file whatever I write in a textfield. I've been able to learn a lot on the web and I've been able to write a specific string into the file,but not the content of the textfield. Can't find any answers for this problem anywhere so I hope you can help.

Here's my code:



I've tried defining value1 in different ways but nothing worked. If I define it as a simple string (like "Hey") it works. So I assume it has to do with the fact that it is a variable. I've looked for other methods to write with BufferedWriter but found nothing that solved the problem.PrintWriter doesn't do the trick either.I guess I'm stuck.

 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Very simplistic and dirty
create a global variable FileWriter (initialized in the main method)
and move your BufferedWriter code from the main method to the actionPerformed method.


Cheers,


 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

From your code, you got the buttons to work ok. Now the question is if you want to write whatever you typed in the text box to the file, shouldn't the BufferedWriter code be inside the actionPerformed() method?

Further to what Carles said, you can also make the File variable global and move the FileWriter and BufferedWriter code inside actionPerformed(). Either way will work.
 
Jhon Calado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the help.

I tried to integrate BufferedWriter in the actionPerformed method and set fw as global but is still not working.




Eclipse was telling me that a need an exception handling in my bw so I took the suggestion and let Eclipse correct it. I can run the frame,and introduce some text in the textfield but I got nothing on the file.

Probably I'm making some basic mistake that I'm not understanding.

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get a NullPointerException? You are using a local FileWriter fw variable in main() where actionPerformed() uses the global variable.
 
Jhon Calado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

thanks for the help.It's working now.

Cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!