2 Replies - 363 Views - Last Post: 17 April 2012 - 06:40 PM Rate Topic: -----

#1 Brandon222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 16-November 08

saving to text file using JBTEXTFIELD

Posted 16 April 2012 - 08:51 PM

ok so im having this problem with my program, its suppose to take the input of the user and place it in a text file. However when i try and grab it i get a null pointerexception. does anyone know what im doing wrong? here is my code:

package windows;

/**
 *
 * @author Alex
 */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class DeleteSoftwareWindow extends JPanel {

    //Title label and combobox
    private JLabel jlblTitle = new JLabel("Title:");
    private JComboBox jcbTitles = new JComboBox(new Object[]{"Office "
                + "2010 Home and Business", "Photoshop",
                "Anti-Malware Lifetime", "Mario Party 9", "Dragon"});
    //Author label and text field
    private JLabel jlblAuthor = new JLabel("Author:");
    private JTextField jtfAuthor = new JTextField(10);
    //Model label and text field
    private JLabel jlblModel = new JLabel("Model:");
    private JTextField jtfModel = new JTextField(10);
    //Button to delete item
    private JButton jbtnDeleteSoftware = new JButton("Delete Software");

    public DeleteSoftwareWindow() {
        //Create subpanel for software properties and add them to this subpanel
        JPanel jpnlSoftware = new JPanel();
        jpnlSoftware.setLayout(new GridLayout(3, 2, 5, 5));
        jpnlSoftware.add(jlblTitle);
        jpnlSoftware.add(jcbTitles);
        jpnlSoftware.add(jlblAuthor);
        jpnlSoftware.add(jtfAuthor);
        jpnlSoftware.add(jlblModel);
        jpnlSoftware.add(jtfModel);
        // Create panel that holds all components: Set to BorderLayout
        JPanel jpnlAll = new JPanel();
        jpnlAll.setLayout(new BorderLayout(5, 10));
        jpnlAll.add(jpnlSoftware, BorderLayout.NORTH);
        jpnlAll.add(jbtnDeleteSoftware, BorderLayout.SOUTH);

        // Add the complete panel to the subframe
        this.add(jpnlAll);

        // Register listener for Submit button using anonymous class listener.
        // Display survey results using JOptionPane message dialog.
        jbtnDeleteSoftware.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, getResults());
            }
        });

    }
    //Return a string consisting of the survey results.

    private String getResults() {
        //Create String Builder
        StringBuilder builder = new StringBuilder();
        //Display Title
        int selectedTitle = jcbTitles.getSelectedIndex();
        builder.append("You deleted: \n");
        // Display the value at selected index
        builder.append(jcbTitles.getModel().getElementAt(selectedTitle));
        builder.append("\n");
        //Display Author
        builder.append("The author is: ");
        builder.append(jtfAuthor.getText());
        builder.append("\n");

        //Display model of product
        builder.append("The model is: ");
        builder.append(jtfModel.getText() + "\n");

        return builder.toString();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: saving to text file using JBTEXTFIELD

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: saving to text file using JBTEXTFIELD

Posted 17 April 2012 - 03:12 AM

There is no such functionality in the code you posted. It displays a message to the user
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: saving to text file using JBTEXTFIELD

Posted 17 April 2012 - 06:40 PM

View PostBrandon222, on 16 April 2012 - 11:51 PM, said:

ok so im having this problem with my program, its suppose to take the input of the user and place it in a text file. However when i try and grab it i get a null pointerexception.

Where ? You don't even import java.io.*;
sure you are not trying to do access a file in that code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1