NullPointerException when reading text file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: NullPointerException when reading text file

Hybrid View

  1. #1
    Andrew McLellan Guest

    NullPointerException when reading text file


    I'm trying to teach myself Java and I'm having a problem reading from a random
    access file. There is too much code to paste here so hopefully the limited
    amount I supply will be enough. What I'm trying to do is read all the records
    from the file and display them in a TextArea. The problem is when I try
    to get the data from the class I get a NullPointerException. I'm not sure
    why since I create a new version of the class. Thanks for you help in advance.

    private void readRecords()
    {
    Record record = new Record();

    DecimalFormat twoDigits = new DecimalFormat( "0.00" );

    try{
    recordDisplay.setText("The records are:\n");

    while(true){
    record.read(file);

    record = (Record) input.readObject();

    if (String.valueOf(record.getRecord()) == " ")
    {
    recordDisplay.append(record.getRecord() +
    "\t" +
    record.getTool() +
    "\t" +
    record.getQuantity() +
    "\t" +
    record.getCost() +
    "\n");
    }
    }
    }
    catch(EOFException eofex)
    {
    //do nothing
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(this,
    "Error " + e,
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }

    }//end readRecords

  2. #2
    Ako Guest

    Re: NullPointerException when reading text file


    Can you send the exact error message that you get and a bit more code?

    Ako




    "Andrew McLellan" <mclellan_a@nospamYahoo.com> wrote:
    >
    >I'm trying to teach myself Java and I'm having a problem reading from a

    random
    >access file. There is too much code to paste here so hopefully the limited
    >amount I supply will be enough. What I'm trying to do is read all the records
    >from the file and display them in a TextArea. The problem is when I try
    >to get the data from the class I get a NullPointerException. I'm not sure
    >why since I create a new version of the class. Thanks for you help in advance.
    >
    >private void readRecords()
    > {
    > Record record = new Record();
    >
    > DecimalFormat twoDigits = new DecimalFormat( "0.00" );
    >
    > try{
    > recordDisplay.setText("The records are:\n");
    >
    > while(true){
    > record.read(file);
    >
    > record = (Record) input.readObject();
    >
    > if (String.valueOf(record.getRecord()) == " ")
    > {
    > recordDisplay.append(record.getRecord() +
    > "\t" +
    > record.getTool() +
    > "\t" +
    > record.getQuantity() +
    > "\t" +
    > record.getCost() +
    > "\n");
    > }
    > }
    > }
    > catch(EOFException eofex)
    > {
    > //do nothing
    > }
    > catch(Exception e)
    > {
    > JOptionPane.showMessageDialog(this,
    > "Error " + e,
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    >
    > }//end readRecords



  3. #3
    Andrew McLellan Guest

    Re: NullPointerException when reading text file


    Thanks Ako
    Here is the main class and the one that I think the error occures. I commented
    the area where the error occurs with the line *****ERROR*****. I’m catching
    the error with the line “catch(Exception e)” so all I get is NullExceptionPointer.
    Thanks for the help.

    *************SeventeenTen Class*************
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.text.DecimalFormat;
    import javax.swing.*;

    public class SeventeenTen extends JFrame {
    private JDesktopPane desktop; //MDI Window
    private JButton open, //opens a file dialog
    updateRecord, //opens a child update window
    newRecord, //opens a child new record window
    allRecord, //opens a child all records window
    deleteRecord; //opens a delete file window
    private JInternalFrame mainDialog; //main child window
    private UpdateDialog updateDialog; //instance of UpdateDialog class
    private NewDialog newDialog; //instance of NewDialog class
    private DeleteDialog deleteDialog; //instance of DeleteDialog class
    private AllRecordsDialog allRecordsDialog; //instance of AllRecordsDialog
    class
    private RandomAccessFile file; //random access file
    private Record record; //instance of Record class
    private Record blank; //creates a blank record

    public SeventeenTen()
    {
    super("Seventeen Ten");

    desktop = new JDesktopPane();

    mainDialog = new JInternalFrame();
    updateRecord = new JButton("Update Record");
    updateRecord.setEnabled(false);
    updateRecord.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    mainDialog.setVisible(false);
    updateDialog.setVisible(true);
    }
    }
    );

    deleteRecord = new JButton("Delete Record");
    deleteRecord.setEnabled(false);
    deleteRecord.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    mainDialog.setVisible(false);
    deleteDialog.setVisible(true);
    }
    }
    );

    newRecord = new JButton("New Record");
    newRecord.setEnabled(false);
    newRecord.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    mainDialog.setVisible(false);
    newDialog.setVisible(true);
    }
    }
    );


    allRecord = new JButton("Show All Records");
    allRecord.setEnabled(false);
    allRecord.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    mainDialog.setVisible(false);
    allRecordsDialog.setVisible(true);
    }
    }
    );


    open = new JButton("Create File");
    open.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    open.setEnabled(false);
    openFile();
    ActionListener l = new ActionListener() {
    public void actionPerformed(ActionEvent e)

    {
    mainDialog.setVisible(true);
    }
    };
    updateDialog = new UpdateDialog(file, l);
    desktop.add(updateDialog);
    updateRecord.setEnabled(true);

    deleteDialog = new DeleteDialog(file, l);
    desktop.add (deleteDialog);
    deleteRecord.setEnabled(true);

    newDialog = new NewDialog(file, l);
    desktop.add(newDialog);
    newRecord.setEnabled(true);

    allRecordsDialog = new AllRecordsDialog(file, l);
    desktop.add(allRecordsDialog);
    allRecordsDialog.setEnabled(true);

    }
    }
    );

    Container c = mainDialog.getContentPane();
    c.setLayout(new GridLayout( 3, 2 ));
    c.add(updateRecord);
    c.add(newRecord);
    c.add(deleteRecord);
    c.add(allRecord);
    c.add(open);

    setSize(500, 400);
    mainDialog.setSize(300, 80);
    mainDialog.setVisible(true);
    desktop.add(mainDialog, BorderLayout.CENTER);
    getContentPane().add(desktop);
    addWindowListener(
    new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    {
    if (file != null)
    closeFile();

    System.exit( 0 );
    }
    }
    );
    show();
    }

    private void openFile()
    {
    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setFileSelectionMode(
    JFileChooser.FILES_ONLY);

    int result = fileChooser.showOpenDialog(this);

    if (result == JFileChooser.CANCEL_OPTION)
    return;

    File fileName = fileChooser.getSelectedFile();

    if (fileName == null ||
    fileName.getName().equals(""))
    JOptionPane.showMessageDialog(this,
    "Invalid File Name",
    "Invalid File Name",
    JOptionPane.ERROR_MESSAGE);
    else {
    // Open the file
    try {
    blank = new Record();
    file = new RandomAccessFile(fileName, "rw");

    for (int i = 0; i < 100; i++)
    blank.write(file);

    updateRecord.setEnabled(true);
    newRecord.setEnabled(true);
    deleteRecord.setEnabled(true);
    allRecord.setEnabled(true);
    open.setEnabled(false);
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(null,
    "File does not exist",
    "Invalid File Name",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (Exception e) {
    JOptionPane.showMessageDialog(null,
    " " + e,
    "Invalid File Name",
    JOptionPane.ERROR_MESSAGE );
    }
    }
    }//end openFile

    private void closeFile()
    {
    try {
    file.close();
    System.exit(0);
    }
    catch(IOException ex) {
    JOptionPane.showMessageDialog(this,
    "Error closing file",
    "Error", JOptionPane.ERROR_MESSAGE);
    System.exit(1);
    }
    }//end closeFile

    public static void main(String args[])
    {
    new SeventeenTen();
    }//end main

    }//end SeventeenTen class



    /************************ U P D A T E D I A L O G ***************************/
    class UpdateDialog extends JInternalFrame {
    private RandomAccessFile file;
    private HardwareUI userInterface;
    private JButton cancel, save;
    private JTextField rec;

    public UpdateDialog(RandomAccessFile f, ActionListener l)
    {
    super("Update Record");

    file = f;
    userInterface = new HardwareUI(4);

    cancel = userInterface.taskOne();
    cancel.setText("Cancel");
    cancel.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    setVisible(false);
    userInterface.clearFields();
    }
    }
    );
    cancel.addActionListener(l);

    save = userInterface.taskTwo();
    save.setText("Save Changes");
    save.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    addRecord(getRecord());
    setVisible(false);
    userInterface.clearFields();
    }
    }
    );
    save.addActionListener(l);

    JTextField transaction =
    userInterface.getFields()[HardwareUI.RECORD];
    transaction.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    try {
    Record record = getRecord();
    double change = Double.parseDouble(
    userInterface.getFieldValues()
    [HardwareUI.RECORD]);
    String[] values = {
    String.valueOf(record.getRecord()),
    record.getTool(),
    String.valueOf(record.getQuantity()),
    String.valueOf(record.getCost()
    + change)
    };

    userInterface.setFieldValues(values);
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(new JFrame(),
    "Invalid Transaction",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    }
    }
    );

    rec = userInterface.getFields()[HardwareUI.RECORD];
    rec.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    Record record = getRecord();

    if ( record.getRecord() != 0 ) {
    String values[] = {
    String.valueOf(record.getRecord()),
    record.getTool(),
    String.valueOf(record.getQuantity()),
    String.valueOf(record.getCost())
    };
    userInterface.setFieldValues(values);
    }
    }
    }
    );
    getContentPane().add(userInterface,
    BorderLayout.CENTER);
    setSize(300, 175);
    setVisible(false);
    }//end UpdateDialog constructor

    private Record getRecord()
    {
    Record record = new Record();

    try {
    int recNumber = Integer.parseInt(rec.getText());


    if (recNumber < 1 || recNumber > 100) {
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    return(record);
    }

    file.seek((recNumber - 1) * Record.size());
    record.read(file);

    if (record.getRecord() == 0)
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(this,
    "Invalid Account",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Reading File",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }

    return record;
    }//end getRecord

    public void addRecord(Record record)
    {
    try {
    int recordNumber = record.getRecord();

    file.seek(( recordNumber - 1 ) * Record.size());
    String[] values = userInterface.getFieldValues();
    record.write(file);
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Writing To File",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(this,
    "Bad Balance",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    }//end addRecord

    }//end UpdateDialog class



    /************************ N E W D I A L O G ***************************/
    class NewDialog extends JInternalFrame {
    private RandomAccessFile file;
    private HardwareUI userInterface;
    private JButton cancel, save;
    private JTextField rec;

    public NewDialog(RandomAccessFile f, ActionListener l)
    {
    super("New Record");

    file = f;
    userInterface = new HardwareUI();

    cancel = userInterface.taskOne();
    cancel.setText( "Cancel" );
    cancel.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    setVisible(false);
    userInterface.clearFields();
    }
    }
    );
    cancel.addActionListener(l);

    rec = userInterface.getFields()[HardwareUI.RECORD];
    save = userInterface.taskTwo();
    save.setText("Save Changes");
    save.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    addRecord(getRecord());
    setVisible(false);
    userInterface.clearFields();
    }
    }
    );
    save.addActionListener(l);

    getContentPane().add(userInterface,
    BorderLayout.CENTER);
    setSize(300, 150);
    setVisible(false);
    }//end NewDialog constructor



    private Record getRecord()
    {
    Record record = new Record();

    try {
    int recNumber = Integer.parseInt(
    rec.getText());

    if (recNumber < 1 || recNumber > 100) {
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    return record;
    }

    file.seek((recNumber - 1 ) * Record.size());
    record.read(file);
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Reading File",
    "Error",
    JOptionPane.ERROR_MESSAGE);
    }

    return record;
    }//end getRecord

    public void addRecord(Record record)
    {
    int recordNumber = 0;
    String[] fields = userInterface.getFieldValues();

    if (record.getRecord() != 0) {
    JOptionPane.showMessageDialog(this,
    "Record Already Exists",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    return;
    }

    try {
    recordNumber =
    Integer.parseInt( fields[HardwareUI.RECORD]);
    record.setRecord(recordNumber);
    record.setTool(fields[ HardwareUI.TOOL]);
    record.setQuantity(Long.parseLong(
    fields[ HardwareUI.QUANTITY]));
    record.setCost(Double.parseDouble(
    fields[HardwareUI.COST]));
    file.seek((recordNumber - 1) * Record.size());
    record.write(file);
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(this,
    "Invalid Balance",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Writing To File",
    "Error",
    JOptionPane.ERROR_MESSAGE);
    }
    }//end addRecord

    }//end class NewDialog



    /************************ D E L E T E D I A L O G ***************************/
    class DeleteDialog extends JInternalFrame {
    private RandomAccessFile file; // file for output
    private HardwareUI userInterface;
    private JButton cancel, delete;
    private JTextField rec;

    public DeleteDialog(RandomAccessFile f, ActionListener l)
    {
    super("Delete Record");

    file = f;
    userInterface = new HardwareUI(1);

    cancel = userInterface.taskOne();
    cancel.setText("Cancel");
    cancel.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    setVisible(false);
    }
    }
    );
    cancel.addActionListener(l);

    delete = userInterface.taskTwo();
    delete.setText("Delete Record");
    delete.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    addRecord(getRecord());
    setVisible(false);
    userInterface.clearFields();
    }
    }
    );
    delete.addActionListener(l);

    rec = userInterface.getFields()[HardwareUI.RECORD];
    rec.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    Record record = getRecord();
    }
    }
    );
    getContentPane().add(userInterface,
    BorderLayout.CENTER);

    setSize(300, 100);
    setVisible(false);
    }

    private Record getRecord()
    {
    Record record = new Record();

    try {
    int recNumber = Integer.parseInt(rec.getText());

    if (recNumber < 1 || recNumber > 100) {
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    return(record);
    }

    file.seek((recNumber - 1) * Record.size());
    record.read(file);

    if (record.getRecord() == 0)
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(this,
    "Account Does Not Exist",
    "Invalid Number Format",
    JOptionPane.ERROR_MESSAGE );
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Reading File",
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }

    return record;
    }//end getRecord

    public void addRecord(Record record)
    {
    if (record.getRecord() == 0)
    return;

    try {
    int recordNumber = record.getRecord();

    file.seek((recordNumber - 1) * Record.size());
    record.setRecord(0);
    record.write(file);
    }
    catch (IOException io) {
    JOptionPane.showMessageDialog(this,
    "Error Writing To File",
    "Error",
    JOptionPane.ERROR_MESSAGE);
    }
    }//end addRecord

    }//end class DeleteDialog



    /************************ A L L R E C O R D S D I A L O G D I A L O G ***************************/
    class AllRecordsDialog extends JInternalFrame {
    private RandomAccessFile file;
    private JTextArea recordDisplay;
    private JButton close, open;
    private JPanel buttonPanel;
    private ObjectInputStream input;
    private String recordType;

    public AllRecordsDialog(RandomAccessFile f, ActionListener l )
    {
    super( "Complete List of Tools" );

    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    buttonPanel = new JPanel();

    close = new JButton("Close");
    close.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    setVisible(false);
    }
    }
    );
    close.addActionListener(l);
    buttonPanel.add(close);

    open = new JButton("Open");
    open.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    readRecords();
    }
    }
    );
    close.addActionListener(l);
    buttonPanel.add(open);

    recordDisplay = new JTextArea();
    JScrollPane scroller = new JScrollPane(recordDisplay);
    c.add(scroller, BorderLayout.CENTER);
    c.add(buttonPanel, BorderLayout.SOUTH);


    file = f;


    //pack();
    setSize(600, 250);
    setVisible(false);
    }//end AllRecordsDialog constructor



    private void readRecords()
    {
    Record record = new Record();

    DecimalFormat twoDigits = new DecimalFormat( "0.00" );

    try{
    recordDisplay.setText("The records are:\n");

    while(true){
    record.read(file);

    *****ERROR*****
    record = (Record) input.readObject();

    if (String.valueOf(record.getRecord()) == " ")
    {
    recordDisplay.append(record.getRecord() +
    "\t" +
    record.getTool() +
    "\t" +
    record.getQuantity() +
    "\t" +
    record.getCost() +
    "\n");
    }
    }
    }
    catch(EOFException eofex)
    {
    //do nothing
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(this,
    "Error " + e,
    "Error",
    JOptionPane.ERROR_MESSAGE );
    }

    }//end readRecords


    }//end class SeventeenTen


    *************Record Class*************
    import java.io.*;

    public class Record extends HardwareRecord {

    public Record()
    {
    this(0, "", 0, 0.0);
    }//end Record

    public Record(int rcd, String tool,
    long qty, double cst)
    {
    super(rcd, tool, qty, cst);
    }//end Record


    public void read( RandomAccessFile file )
    throws IOException
    {
    setRecord(file.readInt());
    setTool(padName(file));
    setQuantity(file.readLong());
    setCost( file.readDouble() );
    }//end read

    private String padName(RandomAccessFile f)
    throws IOException
    {
    char name[] = new char[15], temp;

    for (int i = 0; i < name.length; i++) {
    temp = f.readChar();
    name[i] = temp;
    }

    return new String(name).replace('\0', ' ');
    }//end padName


    public void write( RandomAccessFile file )
    throws IOException
    {
    file.writeInt(getRecord());
    writeName(file, getTool());
    file.writeLong(getQuantity());
    file.writeDouble(getCost());
    }//end write

    private void writeName(RandomAccessFile f, String name)
    throws IOException
    {
    StringBuffer buff = null;

    if (name != null)
    buff = new StringBuffer(name);
    else
    buff = new StringBuffer(15);

    buff.setLength(15);
    f.writeChars(buff.toString());
    }//end writeName

    public static int size()
    {
    return 72;
    }//end size

    }//end record






  4. #4
    Ako Guest

    Re: NullPointerException when reading text file


    I think you are spot on, it looks like it is the line that causes the error.

    If you look at the class AllRecordsDialog you will see that ObjectInputStream
    (input) has only been declared but not initialised properly any where in
    the class and in the line that you have correctly identified you are invoking
    a method on input.

    I hope this will solve your probem.

    Good Luck,

    Ako



    It would probably help if you post the exact error message
    "Andrew McLellan" <mclellan_a@nospamyahoo.com> wrote:
    >
    >Thanks Ako
    >Here is the main class and the one that I think the error occures. I commented
    >the area where the error occurs with the line *****ERROR*****. I’m catching
    >the error with the line “catch(Exception e)” so all I get is NullExceptionPointer.
    > Thanks for the help.
    >
    >*************SeventeenTen Class*************
    >import java.awt.*;
    >import java.awt.event.*;
    >import java.io.*;
    >import java.text.DecimalFormat;
    >import javax.swing.*;
    >
    >public class SeventeenTen extends JFrame {
    > private JDesktopPane desktop; //MDI Window
    > private JButton open, //opens a file dialog
    > updateRecord, //opens a child update window
    > newRecord, //opens a child new record window
    > allRecord, //opens a child all records window
    > deleteRecord; //opens a delete file window
    > private JInternalFrame mainDialog; //main child window
    > private UpdateDialog updateDialog; //instance of UpdateDialog class
    > private NewDialog newDialog; //instance of NewDialog class
    > private DeleteDialog deleteDialog; //instance of DeleteDialog class
    > private AllRecordsDialog allRecordsDialog; //instance of AllRecordsDialog
    >class
    > private RandomAccessFile file; //random access file
    > private Record record; //instance of Record class
    > private Record blank; //creates a blank record
    >
    > public SeventeenTen()
    > {
    > super("Seventeen Ten");
    >
    > desktop = new JDesktopPane();
    >
    > mainDialog = new JInternalFrame();
    > updateRecord = new JButton("Update Record");
    > updateRecord.setEnabled(false);
    > updateRecord.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > mainDialog.setVisible(false);
    > updateDialog.setVisible(true);
    > }
    > }
    > );
    >
    > deleteRecord = new JButton("Delete Record");
    > deleteRecord.setEnabled(false);
    > deleteRecord.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > mainDialog.setVisible(false);
    > deleteDialog.setVisible(true);
    > }
    > }
    > );
    >
    > newRecord = new JButton("New Record");
    > newRecord.setEnabled(false);
    > newRecord.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > mainDialog.setVisible(false);
    > newDialog.setVisible(true);
    > }
    > }
    > );
    >
    >
    > allRecord = new JButton("Show All Records");
    > allRecord.setEnabled(false);
    > allRecord.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > mainDialog.setVisible(false);
    > allRecordsDialog.setVisible(true);
    > }
    > }
    > );
    >
    >
    > open = new JButton("Create File");
    > open.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > open.setEnabled(false);
    > openFile();
    > ActionListener l = new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    >
    > {
    > mainDialog.setVisible(true);
    > }
    > };
    > updateDialog = new UpdateDialog(file, l);
    > desktop.add(updateDialog);
    > updateRecord.setEnabled(true);
    >
    > deleteDialog = new DeleteDialog(file, l);
    > desktop.add (deleteDialog);
    > deleteRecord.setEnabled(true);
    >
    > newDialog = new NewDialog(file, l);
    > desktop.add(newDialog);
    > newRecord.setEnabled(true);
    >
    > allRecordsDialog = new AllRecordsDialog(file, l);
    > desktop.add(allRecordsDialog);
    > allRecordsDialog.setEnabled(true);
    >
    > }
    > }
    > );
    >
    > Container c = mainDialog.getContentPane();
    > c.setLayout(new GridLayout( 3, 2 ));
    > c.add(updateRecord);
    > c.add(newRecord);
    > c.add(deleteRecord);
    > c.add(allRecord);
    > c.add(open);
    >
    > setSize(500, 400);
    > mainDialog.setSize(300, 80);
    > mainDialog.setVisible(true);
    > desktop.add(mainDialog, BorderLayout.CENTER);
    > getContentPane().add(desktop);
    > addWindowListener(
    > new WindowAdapter() {
    > public void windowClosing(WindowEvent e)
    > {
    > if (file != null)
    > closeFile();
    >
    > System.exit( 0 );
    > }
    > }
    > );
    > show();
    > }
    >
    > private void openFile()
    > {
    > JFileChooser fileChooser = new JFileChooser();
    >
    > fileChooser.setFileSelectionMode(
    > JFileChooser.FILES_ONLY);
    >
    > int result = fileChooser.showOpenDialog(this);
    >
    > if (result == JFileChooser.CANCEL_OPTION)
    > return;
    >
    > File fileName = fileChooser.getSelectedFile();
    >
    > if (fileName == null ||
    > fileName.getName().equals(""))
    > JOptionPane.showMessageDialog(this,
    > "Invalid File Name",
    > "Invalid File Name",
    > JOptionPane.ERROR_MESSAGE);
    > else {
    > // Open the file
    > try {
    > blank = new Record();
    > file = new RandomAccessFile(fileName, "rw");
    >
    > for (int i = 0; i < 100; i++)
    > blank.write(file);
    >
    > updateRecord.setEnabled(true);
    > newRecord.setEnabled(true);
    > deleteRecord.setEnabled(true);
    > allRecord.setEnabled(true);
    > open.setEnabled(false);
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(null,
    > "File does not exist",
    > "Invalid File Name",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (Exception e) {
    > JOptionPane.showMessageDialog(null,
    > " " + e,
    > "Invalid File Name",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > }
    > }//end openFile
    >
    > private void closeFile()
    > {
    > try {
    > file.close();
    > System.exit(0);
    > }
    > catch(IOException ex) {
    > JOptionPane.showMessageDialog(this,
    > "Error closing file",
    > "Error", JOptionPane.ERROR_MESSAGE);
    > System.exit(1);
    > }
    > }//end closeFile
    >
    > public static void main(String args[])
    > {
    > new SeventeenTen();
    > }//end main
    >
    >}//end SeventeenTen class
    >
    >
    >
    >/************************ U P D A T E D I A L O G ***************************/
    >class UpdateDialog extends JInternalFrame {
    > private RandomAccessFile file;
    > private HardwareUI userInterface;
    > private JButton cancel, save;
    > private JTextField rec;
    >
    > public UpdateDialog(RandomAccessFile f, ActionListener l)
    > {
    > super("Update Record");
    >
    > file = f;
    > userInterface = new HardwareUI(4);
    >
    > cancel = userInterface.taskOne();
    > cancel.setText("Cancel");
    > cancel.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > setVisible(false);
    > userInterface.clearFields();
    > }
    > }
    > );
    > cancel.addActionListener(l);
    >
    > save = userInterface.taskTwo();
    > save.setText("Save Changes");
    > save.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > addRecord(getRecord());
    > setVisible(false);
    > userInterface.clearFields();
    > }
    > }
    > );
    > save.addActionListener(l);
    >
    > JTextField transaction =
    > userInterface.getFields()[HardwareUI.RECORD];
    > transaction.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > try {
    > Record record = getRecord();
    > double change = Double.parseDouble(
    > userInterface.getFieldValues()
    > [HardwareUI.RECORD]);
    > String[] values = {
    > String.valueOf(record.getRecord()),
    > record.getTool(),
    > String.valueOf(record.getQuantity()),
    > String.valueOf(record.getCost()
    > + change)
    > };
    >
    > userInterface.setFieldValues(values);
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(new JFrame(),
    > "Invalid Transaction",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > }
    > }
    > );
    >
    > rec = userInterface.getFields()[HardwareUI.RECORD];
    > rec.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > Record record = getRecord();
    >
    > if ( record.getRecord() != 0 ) {
    > String values[] = {
    > String.valueOf(record.getRecord()),
    > record.getTool(),
    > String.valueOf(record.getQuantity()),
    > String.valueOf(record.getCost())
    > };
    > userInterface.setFieldValues(values);
    > }
    > }
    > }
    > );
    > getContentPane().add(userInterface,
    > BorderLayout.CENTER);
    > setSize(300, 175);
    > setVisible(false);
    > }//end UpdateDialog constructor
    >
    > private Record getRecord()
    > {
    > Record record = new Record();
    >
    > try {
    > int recNumber = Integer.parseInt(rec.getText());
    >
    >
    > if (recNumber < 1 || recNumber > 100) {
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > return(record);
    > }
    >
    > file.seek((recNumber - 1) * Record.size());
    > record.read(file);
    >
    > if (record.getRecord() == 0)
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(this,
    > "Invalid Account",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Reading File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    >
    > return record;
    > }//end getRecord
    >
    > public void addRecord(Record record)
    > {
    > try {
    > int recordNumber = record.getRecord();
    >
    > file.seek(( recordNumber - 1 ) * Record.size());
    > String[] values = userInterface.getFieldValues();
    > record.write(file);
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Writing To File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(this,
    > "Bad Balance",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > }//end addRecord
    >
    >}//end UpdateDialog class
    >
    >
    >
    >/************************ N E W D I A L O G ***************************/
    >class NewDialog extends JInternalFrame {
    > private RandomAccessFile file;
    > private HardwareUI userInterface;
    > private JButton cancel, save;
    > private JTextField rec;
    >
    > public NewDialog(RandomAccessFile f, ActionListener l)
    > {
    > super("New Record");
    >
    > file = f;
    > userInterface = new HardwareUI();
    >
    > cancel = userInterface.taskOne();
    > cancel.setText( "Cancel" );
    > cancel.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > setVisible(false);
    > userInterface.clearFields();
    > }
    > }
    > );
    > cancel.addActionListener(l);
    >
    > rec = userInterface.getFields()[HardwareUI.RECORD];
    > save = userInterface.taskTwo();
    > save.setText("Save Changes");
    > save.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > addRecord(getRecord());
    > setVisible(false);
    > userInterface.clearFields();
    > }
    > }
    > );
    > save.addActionListener(l);
    >
    > getContentPane().add(userInterface,
    > BorderLayout.CENTER);
    > setSize(300, 150);
    > setVisible(false);
    > }//end NewDialog constructor
    >
    >
    >
    > private Record getRecord()
    > {
    > Record record = new Record();
    >
    > try {
    > int recNumber = Integer.parseInt(
    > rec.getText());
    >
    > if (recNumber < 1 || recNumber > 100) {
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > return record;
    > }
    >
    > file.seek((recNumber - 1 ) * Record.size());
    > record.read(file);
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Reading File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE);
    > }
    >
    > return record;
    > }//end getRecord
    >
    > public void addRecord(Record record)
    > {
    > int recordNumber = 0;
    > String[] fields = userInterface.getFieldValues();
    >
    > if (record.getRecord() != 0) {
    > JOptionPane.showMessageDialog(this,
    > "Record Already Exists",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > return;
    > }
    >
    > try {
    > recordNumber =
    > Integer.parseInt( fields[HardwareUI.RECORD]);
    > record.setRecord(recordNumber);
    > record.setTool(fields[ HardwareUI.TOOL]);
    > record.setQuantity(Long.parseLong(
    > fields[ HardwareUI.QUANTITY]));
    > record.setCost(Double.parseDouble(
    > fields[HardwareUI.COST]));
    > file.seek((recordNumber - 1) * Record.size());
    > record.write(file);
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(this,
    > "Invalid Balance",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Writing To File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE);
    > }
    > }//end addRecord
    >
    >}//end class NewDialog
    >
    >
    >
    >/************************ D E L E T E D I A L O G ***************************/
    >class DeleteDialog extends JInternalFrame {
    > private RandomAccessFile file; // file for output
    > private HardwareUI userInterface;
    > private JButton cancel, delete;
    > private JTextField rec;
    >
    > public DeleteDialog(RandomAccessFile f, ActionListener l)
    > {
    > super("Delete Record");
    >
    > file = f;
    > userInterface = new HardwareUI(1);
    >
    > cancel = userInterface.taskOne();
    > cancel.setText("Cancel");
    > cancel.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > setVisible(false);
    > }
    > }
    > );
    > cancel.addActionListener(l);
    >
    > delete = userInterface.taskTwo();
    > delete.setText("Delete Record");
    > delete.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > addRecord(getRecord());
    > setVisible(false);
    > userInterface.clearFields();
    > }
    > }
    > );
    > delete.addActionListener(l);
    >
    > rec = userInterface.getFields()[HardwareUI.RECORD];
    > rec.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > Record record = getRecord();
    > }
    > }
    > );
    > getContentPane().add(userInterface,
    > BorderLayout.CENTER);
    >
    > setSize(300, 100);
    > setVisible(false);
    > }
    >
    > private Record getRecord()
    > {
    > Record record = new Record();
    >
    > try {
    > int recNumber = Integer.parseInt(rec.getText());
    >
    > if (recNumber < 1 || recNumber > 100) {
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > return(record);
    > }
    >
    > file.seek((recNumber - 1) * Record.size());
    > record.read(file);
    >
    > if (record.getRecord() == 0)
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (NumberFormatException nfe) {
    > JOptionPane.showMessageDialog(this,
    > "Account Does Not Exist",
    > "Invalid Number Format",
    > JOptionPane.ERROR_MESSAGE );
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Reading File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    >
    > return record;
    > }//end getRecord
    >
    > public void addRecord(Record record)
    > {
    > if (record.getRecord() == 0)
    > return;
    >
    > try {
    > int recordNumber = record.getRecord();
    >
    > file.seek((recordNumber - 1) * Record.size());
    > record.setRecord(0);
    > record.write(file);
    > }
    > catch (IOException io) {
    > JOptionPane.showMessageDialog(this,
    > "Error Writing To File",
    > "Error",
    > JOptionPane.ERROR_MESSAGE);
    > }
    > }//end addRecord
    >
    >}//end class DeleteDialog
    >
    >
    >
    >/************************ A L L R E C O R D S D I A L O G D I A L O G

    ***************************/
    >class AllRecordsDialog extends JInternalFrame {
    > private RandomAccessFile file;
    > private JTextArea recordDisplay;
    > private JButton close, open;
    > private JPanel buttonPanel;
    > private ObjectInputStream input;
    > private String recordType;
    >
    > public AllRecordsDialog(RandomAccessFile f, ActionListener l )
    > {
    > super( "Complete List of Tools" );
    >
    > Container c = getContentPane();
    > c.setLayout(new BorderLayout());
    > buttonPanel = new JPanel();
    >
    > close = new JButton("Close");
    > close.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > setVisible(false);
    > }
    > }
    > );
    > close.addActionListener(l);
    > buttonPanel.add(close);
    >
    > open = new JButton("Open");
    > open.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent e)
    > {
    > readRecords();
    > }
    > }
    > );
    > close.addActionListener(l);
    > buttonPanel.add(open);
    >
    > recordDisplay = new JTextArea();
    > JScrollPane scroller = new JScrollPane(recordDisplay);
    > c.add(scroller, BorderLayout.CENTER);
    > c.add(buttonPanel, BorderLayout.SOUTH);
    >
    >
    > file = f;
    >
    >
    > //pack();
    > setSize(600, 250);
    > setVisible(false);
    > }//end AllRecordsDialog constructor
    >
    >
    >
    > private void readRecords()
    > {
    > Record record = new Record();
    >
    > DecimalFormat twoDigits = new DecimalFormat( "0.00" );
    >
    > try{
    > recordDisplay.setText("The records are:\n");
    >
    > while(true){
    > record.read(file);
    >
    > *****ERROR*****
    > record = (Record) input.readObject();
    >
    > if (String.valueOf(record.getRecord()) == " ")
    > {
    > recordDisplay.append(record.getRecord() +
    > "\t" +
    > record.getTool() +
    > "\t" +
    > record.getQuantity() +
    > "\t" +
    > record.getCost() +
    > "\n");
    > }
    > }
    > }
    > catch(EOFException eofex)
    > {
    > //do nothing
    > }
    > catch(Exception e)
    > {
    > JOptionPane.showMessageDialog(this,
    > "Error " + e,
    > "Error",
    > JOptionPane.ERROR_MESSAGE );
    > }
    >
    > }//end readRecords
    >
    >
    > }//end class SeventeenTen
    >
    >
    >*************Record Class*************
    >import java.io.*;
    >
    >public class Record extends HardwareRecord {
    >
    > public Record()
    > {
    > this(0, "", 0, 0.0);
    > }//end Record
    >
    > public Record(int rcd, String tool,
    > long qty, double cst)
    > {
    > super(rcd, tool, qty, cst);
    > }//end Record
    >
    >
    > public void read( RandomAccessFile file )
    > throws IOException
    > {
    > setRecord(file.readInt());
    > setTool(padName(file));
    > setQuantity(file.readLong());
    > setCost( file.readDouble() );
    > }//end read
    >
    > private String padName(RandomAccessFile f)
    > throws IOException
    > {
    > char name[] = new char[15], temp;
    >
    > for (int i = 0; i < name.length; i++) {
    > temp = f.readChar();
    > name[i] = temp;
    > }
    >
    > return new String(name).replace('\0', ' ');
    > }//end padName
    >
    >
    > public void write( RandomAccessFile file )
    > throws IOException
    > {
    > file.writeInt(getRecord());
    > writeName(file, getTool());
    > file.writeLong(getQuantity());
    > file.writeDouble(getCost());
    > }//end write
    >
    > private void writeName(RandomAccessFile f, String name)
    > throws IOException
    > {
    > StringBuffer buff = null;
    >
    > if (name != null)
    > buff = new StringBuffer(name);
    > else
    > buff = new StringBuffer(15);
    >
    > buff.setLength(15);
    > f.writeChars(buff.toString());
    > }//end writeName
    >
    > public static int size()
    > {
    > return 72;
    > }//end size
    >
    >}//end record
    >
    >
    >
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center