• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferedWriter - writing a variable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

thanks for the help.It's working now.

Cheers
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic