Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem with Formatters (I think)

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with Formatters (I think)

    Hi!
    I'm trying to make a program with gui. The window should have 2 textfields and one button. One of the textfield are uneditable. When the button is clicked i want the stuff from the first textfield to be written to a file and I want the second textfield to output the text from the file. The gui works fine, but whenever I press the button i get a bunch of errors. This is the code I'm using:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class GUI extends JFrame{
    	private JTextField toFile;
    	private JTextField fromFile;
    	private JButton sendToFile;
    	private FileHandling fh;
     
    	public GUI(){
    	super("File");
    	setLayout(new FlowLayout());
     
    	toFile = new JTextField("Write what you want to send to the file",20);
    	add(toFile);
     
    	sendToFile=new JButton("Add to file");
    	add (sendToFile);
     
    	fh=new FileHandling();
    	fh.checkFile();
    	fh.readFile();
     
    	fromFile=new JTextField(fh.fromFile,20);
    	fromFile.setEditable(false);
    	add(fromFile);
     
    	HandlerClass hc=new HandlerClass();
    	sendToFile.addActionListener(hc);
    }
    	private class HandlerClass implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			String a=fromFile.getText();
    			fh.writeFile(a);
    		}
    	}
    }

    import java.io.*;
    import java.util.*;
     
    public class FileHandling {
    	private Formatter x;
    	private Scanner s;
    	private File y;
    	public String fromFile;
     
    	public void checkFile(){
    		y=new File("data.txt");
    		if(y.exists()){
    			try{
    				s=new Scanner(y);
    			}
    			catch(Exception e){
    				System.out.println("Could not scan the file");
    			}
    		}else{
    			try{
    				x=new Formatter("data.txt");
    				s=new Scanner(y);
    			}
    			catch(Exception e){
    				System.out.println("Could not create the file");
    			}
    		}
    	}
    	public void readFile(){
    		while(s.hasNextLine()){
    			fromFile=s.nextLine();
    		}
    	}
    	public void writeFile(String a){
    		x.format("%s \n", a);
    	}
    	public void closeFile(){
    		x.close();
    	}
    }

    If anyone can tell me what I'm doing wrong I would be very grateful.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with Formatters (I think)

    Do you think that posting the error messages might help?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Formatters (I think)

    Quote Originally Posted by Junky View Post
    Do you think that posting the error messages might help?
    Sure
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at FileHandling.writeFile(FileHandling.java:35)
    at GUI$HandlerClass.actionPerformed(GUI.java:37)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)

    The problem is that these are just half of the error messages I get.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Formatters (I think)

    A NullPointerException means something is null. What is null on line 35 of the FileHandling class? What variables do you access on that line? Add some println's to see. Once you determine what is null, backtrack through the code to determine why it was never initialized and determine the most appropriate way to initialize.

  5. The Following User Says Thank You to copeg For This Useful Post:

    sindreb (June 26th, 2013)

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Formatters (I think)

    Thanks! Found out I never initialized the Formatter if the file did'nt exist. Now my problem is that the second textfield, fromFile, does not display anything. Any clue on how I can fix that?

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Formatters (I think)

    Quote Originally Posted by sindreb View Post
    Thanks! Found out I never initialized the Formatter if the file did'nt exist. Now my problem is that the second textfield, fromFile, does not display anything. Any clue on how I can fix that?
    Do you ever set the text of the JTextField?

  8. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Formatters (I think)

    Quote Originally Posted by copeg View Post
    Do you ever set the text of the JTextField?
    Yeah, it is automaticly filled with "Write what you want to send to the file". I also checked a bit, and the file data.txt is being created, but it never gets any data. I also tried to put some text in the file, but it did not show up in the second textfield. I have really no idea why it does not work :/

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with Formatters (I think)

    If you have no content in the output file the most common mistake is not closing the Writer.
    Improving the world one idiot at a time!

  10. The Following User Says Thank You to Junky For This Useful Post:

    sindreb (June 27th, 2013)

  11. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Formatters (I think)

    So, how do I close the writer? I'm sorry, but I'm kinda new to java

  12. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with Formatters (I think)

    Did you bother to read the API to see what methods the class has?
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  3. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM

Tags for this Thread