Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:17:00+00:00 2026-06-13T10:17:00+00:00

I’m working on a program that saves the contents of four text fields and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:17:01+00:00Added an answer on June 13, 2026 at 10:17 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.