Results 1 to 14 of 14
  1. #1
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default 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:
    Java Code:
    public class event implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			ReadWrite wr = new ReadWrite();
    			wr.write(file2, te.getText());
    		}
    	}
    Here are the errors I am getting

    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)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing To A TextFile From A JTextArea

    Which line causes the NullPointerException (NPE) to be thrown? Could it be this one?

    Java Code:
    wr.write(file2, te.getText());
    if so, is te null? Have you tested to see if it is?

  3. #3
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by Fubarable View Post
    Which line causes the NullPointerException (NPE) to be thrown? Could it be this one?

    Java Code:
    wr.write(file2, te.getText());
    if so, is te null? Have you tested to see if it is?
    hmm I didn't test to see that I don't think it is but I will try it right now I need to fix something in my code and then I will try what you recommended.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by Fubarable View Post
    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.
    They do? How do I use it?

  6. #6
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by anthropamorphic View Post
    hmm I didn't test to see that I don't think it is but I will try it right now I need to fix something in my code and then I will try what you recommended.
    You were right! It isn't getting the text from the textarea for some reason? I though I was suppose to just use (name of textarea).getText(); is there a different way to do it?

  7. #7
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by anthropamorphic View Post
    You were right! It isn't getting the text from the textarea for some reason? I though I was suppose to just use (name of textarea).getText(); is there a different way to do it?
    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.

  9. #9
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by Fubarable View Post
    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.
    Would you want me to post my code?
    the the variable is this:
    Java Code:
    JTextArea te;
    Java Code:
    JTextArea te = new JTextArea(out , 3, 5);
    frame.add(te, BorderLayout.CENTER);
    te.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    That's all the code that relates to the the variable.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by anthropamorphic View Post
    Would you want me to post my code?
    the the variable is this:
    Java Code:
    JTextArea te;
    Java Code:
    JTextArea te = new JTextArea(out , 3, 5);
    frame.add(te, BorderLayout.CENTER);
    te.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    That's all the code that relates to the the variable.
    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. ;)

  11. #11
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default 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);
    		}
    	}
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by anthropamorphic View Post
    Alright here is the class I am having trouble with:

    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:


    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??
    			
    			
    			// ....
    		}
    	}
    }
    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;  // 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.

  13. #13
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    136
    Rep Power
    0

    Default 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!

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing To A TextFile From A JTextArea

    Quote Originally Posted by anthropamorphic View Post
    Wow I feel like a complete idiot. Thank you so much I can't believe I did that!
    If you're an idiot, then you're in good company, as we've all fallen for this trap. And you're quite welcome.

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 04:02 PM
  2. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  3. Reading a textfile
    By Bomber_Will in forum New To Java
    Replies: 8
    Last Post: 01-15-2010, 09:18 AM
  4. textfile into arrays.
    By Sticks_ll in forum New To Java
    Replies: 27
    Last Post: 03-16-2009, 08:00 AM
  5. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •