user1699107
user1699107

Reputation: 81

Saving a text field/area to a text file

I'm working on a program that saves the contents of four text fields and a text area and saves them to a text file when the "Send" button is clicked. The output should look something like this:

To: [Text Field]
CC: [Second Text Field]
Bcc: [Third Text Field]
Subject: [Fourth Text Field]
Message:
[Text Area]

For example, the first line of the text file will have "To: ", the contents of a text field, and will then skip to the next line.

Although my program compiles, the text file remains blank. I've tried everything I can think of, but can't seem to figure out the cause. Here's my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class EmailProg extends JFrame implements ActionListener {
    private JPanel panNorth;
    private JPanel panCenter;
    private JPanel panSouth;

    private JLabel toLabel;
    private JLabel ccLabel;
    private JLabel bccLabel;
    private JLabel subLabel;
    private JLabel msgLabel;

    private JTextField toField;
    private JTextField ccField;
    private JTextField bccField;
    private JTextField subField;
    private JTextArea msgArea;

    private JButton send;


    public EmailProg() {
        setTitle("Compose Email");
        setLayout(new BorderLayout());

        panNorth = new JPanel();
        panNorth.setLayout(new GridLayout(4, 2));
        JLabel toLabel = new JLabel("To:");
        panNorth.add(toLabel);
        JTextField toField = new JTextField(15);
        panNorth.add(toField);
        JLabel ccLabel = new JLabel("CC:");
        panNorth.add(ccLabel);
        JTextField ccField = new JTextField(15);
        panNorth.add(ccField);
        JLabel bccLabel = new JLabel("Bcc:");
        panNorth.add(bccLabel);
        JTextField bccField = new JTextField(15);
        panNorth.add(bccField);
        JLabel subLabel = new JLabel("Subject:");
        panNorth.add(subLabel);
        JTextField subField = new JTextField(15);
        panNorth.add(subField);
        add(panNorth, BorderLayout.NORTH);

        panCenter = new JPanel();
        panCenter.setLayout(new GridLayout(2, 1));
        JLabel msgLabel = new JLabel("Message:");
        panCenter.add(msgLabel);
        JTextArea msgArea = new JTextArea(5, 15);
        panCenter.add(msgArea);
        add(panCenter, BorderLayout.CENTER);

        panSouth = new JPanel();
        panSouth.setLayout(new FlowLayout());
        JButton send = new JButton("Send");
        send.addActionListener(this);
        panSouth.add(send);
        add(panSouth, BorderLayout.SOUTH);


    }

        public void actionPerformed (ActionEvent event) {
            try {
                //Write labels and corresponding fields to text file
                BufferedWriter outfile = new BufferedWriter(new FileWriter("email.txt"));
                outfile.write("To: ");
                outfile.write(toField.getText());
                //repeat for CC, Bcc, and Subject labels/fields, and for Message label/text area


            }
            catch(FileNotFoundException e) {
                System.out.println("File not found.");
            }
            catch(NullPointerException j){
                System.out.println("Null.");
            }
            catch(IOException k){
                System.out.println("IO Exception.");            
            }
            JOptionPane.showMessageDialog(this,"Saved");
        }


    public static void main(String[] args) {
        EmailProg win = new EmailProg();
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.pack();
        win.setVisible(true);
    }
}

Thanks in advance for any help you can offer.

Upvotes: 2

Views: 9587

Answers (2)

Reimeus
Reimeus

Reputation: 159754

The line

outfile.write(toField.getText());

if causing the application to enter the NullPointerException code block. This is because the variable toField is not set.

To fix, you can to remove the JTextField keyword from the declaration of toField to in your constructor to assign it to the class member variable:

toField = new JTextField(15);

Similarly for the other components in the application that you may need outside the scope of the constructor:

    ccLabel = new JLabel("CC:");
    ccField = new JTextField(15);
    // etc.

Also another important note—don't forget to close the output stream after writing, otherwise any data that was buffered to be written will not be written to disk.

outfile.close();

Upvotes: 2

Yogendra Singh
Yogendra Singh

Reputation: 34367

Change JTextField toField = new JTextField(15); to toField = new JTextField(15);. This will initialize your class member variable instead of creating a new local variable.

Since you have redefined it, the class level variable(toField) being used in actionPerformed() is not coming as initialized and hence the issue.

Upvotes: 1

Related Questions