Results 1 to 14 of 14
- 09-11-2011, 02:07 AM #1
Writing To A TextFile From A JTextArea
Hello, I have been having this problem for a while now, but my problem is I can't
seem to be able to write to a text file if I get the text from a JTextField. I have tried so many different approaches to it and I can't seem to figure it out.
This is what I am using to write to the file:
Java Code:public void write(File file, String string){ try { BufferedWriter Te = null; System.out.println(file); Te = new BufferedWriter(new FileWriter(file)); System.out.println("BufferWriter and FileWriter Obtained File and it should be writing " + string); Te.write(string); Te.newLine(); Te.close(); System.out.println("Success While Writer"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("error when writing to a file"); } }
and here is part of my class that is using this subroutine:
Here are the errors I am gettingJava Code:public class event implements ActionListener{ public void actionPerformed(ActionEvent e){ ReadWrite wr = new ReadWrite(); wr.write(file2, te.getText()); } }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Open$event.actionPerformed(Open.java:43)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6373)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:613 8)
at java.awt.Container.processEvent(Container.java:208 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4735)
at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
at java.awt.Component.dispatchEvent(Component.java:45 65)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:45 65)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 649)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
-
Re: Writing To A TextFile From A JTextArea
Which line causes the NullPointerException (NPE) to be thrown? Could it be this one?
if so, is te null? Have you tested to see if it is?Java Code:wr.write(file2, te.getText());
- 09-11-2011, 02:25 AM #3
-
Re: Writing To A TextFile From A JTextArea
Also, you know that text components like JTextFields and JTextAreas have the write method, an innate facility for writing out the text contained to a text file.
- 09-11-2011, 02:43 AM #5
- 09-11-2011, 02:47 AM #6
- 09-11-2011, 02:48 AM #7
Re: Writing To A TextFile From A JTextArea
I think I am suppose to use a document listener I will try it and tell you if it worked.
-
Re: Writing To A TextFile From A JTextArea
It has nothing to do with variable name and all to do with object reference. Your ta variable in that method is not referencing any object much less the valid JTextArea of interest. There's no way of knowing why based on the code you're posting. Sometimes we see this when newbies inappropriately extend a class to access its variables, but again, I have no idea if this is what is happening in your code.
- 09-11-2011, 02:55 AM #9
Re: Writing To A TextFile From A JTextArea
Would you want me to post my code?
the the variable is this:
Java Code:JTextArea te;
That's all the code that relates to the the variable.Java Code:JTextArea te = new JTextArea(out , 3, 5); frame.add(te, BorderLayout.CENTER); te.setBorder(BorderFactory.createLineBorder(Color.BLACK));
-
Re: Writing To A TextFile From A JTextArea
What you've posted in no way shows us what your problem is. Don't post all the code, just enough code so we can solve the problem. I know -- it's not easy to know what that is, and that's part of the art of asking questions here. Don't worry, you'll get better at this skill with time. ;)
- 09-11-2011, 03:04 AM #11
Re: Writing To A TextFile From A JTextArea
Alright here is the class I am having trouble with:
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class Open { JButton save; JTextArea te; File file2; public void open(File file){ ReadWrite wr = new ReadWrite(); file2 = file; List l = new List(); wr.read(file); System.out.println("File Loaded = " + file); String out = wr.output; System.out.println(out); System.out.println(wr.output); JTextArea te = new JTextArea(out , 3, 5); JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.setSize(330, 345); frame.setVisible(true); frame.add(te, BorderLayout.CENTER); JPanel pa = new JPanel(); pa.setBackground(Color.LIGHT_GRAY); te.setBorder(BorderFactory.createLineBorder(Color.BLACK)); frame.add(pa, BorderLayout.PAGE_START); JButton save = new JButton(" Save "); pa.setForeground(Color.BLACK); pa.setBorder(BorderFactory.createLineBorder(Color.BLACK)); pa.add(save); event e = new event(); save.addActionListener(e); } public class event implements ActionListener{ public void actionPerformed(ActionEvent e){ Open op = new Open(); String tein = te.getText(); System.out.println("The File You Are Saving is" + file2 + "\n" + "The Text You Are Saving In The File Is" + tein); ReadWrite wr = new ReadWrite(); wr.write(file2, tein); } } }
-
Re: Writing To A TextFile From A JTextArea
Geeze, that Norm must have a crystal ball because we were just now discussing this issue in a different thread. Your problem is one of variable shadowing, that you have two te variables, one that refers to a valid JTextArea object, and one that is null. Check out your code:
The solution is not to re-declare the variable in the open method. Instead do this:Java Code://... public class Open { JButton save; JTextArea te; // here is the class field that is never initialized File file2; public void open(File file){ // .... // and here is the shadowed variable that is declared in the open method and only visible inside the open method: JTextArea te = new JTextArea(out , 3, 5); // .... } public class event implements ActionListener{ public void actionPerformed(ActionEvent e){ Open op = new Open(); String tein = te.getText(); // guess which te variable is being used here?? // .... } } }
Java Code://... public class Open { JButton save; JTextArea te; // te declared once here File file2; public void open(File file){ // .... // ******* don't re-declare te ******* te = new JTextArea(out , 3, 5); // te initialized but NOT declared here // .... }Last edited by Fubarable; 09-11-2011 at 03:21 AM.
- 09-11-2011, 03:17 AM #13
Re: Writing To A TextFile From A JTextArea
Wow I feel like a complete idiot. Thank you so much I can't believe I did that!
-
Similar Threads
-
ECG plot in a textfile
By samson in forum Java 2DReplies: 13Last Post: 01-28-2012, 04:02 PM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Reading a textfile
By Bomber_Will in forum New To JavaReplies: 8Last Post: 01-15-2010, 09:18 AM -
textfile into arrays.
By Sticks_ll in forum New To JavaReplies: 27Last Post: 03-16-2009, 08:00 AM -
Textfile and GUI problems
By saytri in forum New To JavaReplies: 2Last Post: 12-21-2007, 04:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks