Some Help needed trying to write to an array via GUI using File IO

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

56 Replies - 1572 Views - Last Post: 03 December 2011 - 05:27 PM Rate Topic: -----

#1 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 07:03 AM

Hello,

I am creating a small GUI that will allow a user to store a customers information. I am not getting any error messages yet nothing seems to be saving to file, can anybody help me. The code below is posted for a few classes I have.

import java.util.*;
import java.io.*;


public class CustomerIO {

 private ArrayList < Customer > customers;



  public static ArrayList<Customer> readFromFile()
    {
        
        ObjectInputStream in = null;
        ArrayList<Customer> customers = new ArrayList<Customer>();
        try
            {
            in = new ObjectInputStream(new
                                       BufferedInputStream(new
                                             FileInputStream("customerfile.txt")));
            
            ArrayList<Customer> readObject = (ArrayList<Customer>)in.readObject();
			customers = readObject;
            
            }
        catch( Exception e)
        {
        }
        finally { 
                try{
                in.close(); 
                   }   
               catch (Exception e) { 
            }}
        return customers;
        
          
    
    
        
            
    
  }
    
 public static void writeToFile(ArrayList<Customer> customers)
    {
        ObjectOutputStream out = null;
        try
        {
            out = new ObjectOutputStream(new
            BufferedOutputStream(new
            FileOutputStream("customerfile.txt")));
            out.writeObject(customers);
        }
        catch( Exception e) {}
        finally { 
            try{                out.close(); }
            catch( Exception e) {}
        }
    }
    
}


Gui classes is below.


import CustomerClasses.*;
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
import java.util.*; 

public class CreateCustomerGUI extends JFrame implements ActionListener {


private JPanel createCustomerPanel;
private JLabel familyName;
private JLabel customerID;
private JLabel firstName;
private JLabel postcode;
private JLabel dateOfBirth;
private JLabel houseNumberOrName;
private JLabel street;
private JLabel town;
private JLabel county;
private JButton createCustomer;
private JTextField customerIDText;
private JTextField familyNameText;
private JTextField firstNameText;
private JTextField houseNumberOrNameText;
private JTextField streetText;
private JTextField townText;
private JTextField countyText;
private JTextField postcodeText;
private JTextField dateOfBirthText;
private CustomerIO customers;
MenuControlCustomers control;

    public CreateCustomerGUI()
    {
       super("");
       makeFrame();
       showFrame();
       
    }
    
    public CreateCustomerGUI(MenuControlCustomers control)

    {
        this();
        this.control = control;
        
    }
    
    public void run()
	{
	 this.setVisible(true);
	}
	
	
	public void writeToFile()
	{
	    control.writeToFile();
	}
	
	public  void addToCustomers()
	{
	     	        
	    control.addToCustomers(firstNameText.getText(),familyNameText.getText(),dateOfBirthText.getText(),  houseNumberOrNameText.getText()
	    ,streetText.getText(), townText.getText(), countyText.getText(), postcodeText.getText());
	}
    
    private void showFrame()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        setSize(800,400);
        setLocationRelativeTo( null); 
        setResizable(false);
        setVisible(true);
    }
    
      private void makeFrame()
    
    {
        setLayout(new GridLayout(1,1));
        makePanels();
        addPanels();
        
    }
    
    public void makePanels()
    
    {
        makeCreateCustomerPanel();
        
        
     }
     
    private void addPanels()
    {
        add(createCustomerPanel,BorderLayout.CENTER);
        
        
    }
    
     private void makeCreateCustomerPanel()
    
    {   createCustomerPanel = new JPanel();
        createCustomerPanel.setLayout(new GridLayout(10,2));
        customerID = new JLabel("Customer ID:", JLabel.RIGHT);
        customerIDText = new JTextField(10);
        firstName = new JLabel("First Name:",JLabel.RIGHT);
        firstNameText = new JTextField(10);
        familyName = new JLabel("Family Name:",JLabel.RIGHT);
        familyNameText = new JTextField(10);
        dateOfBirth = new JLabel("Date Of Birth:",JLabel.RIGHT);
        dateOfBirthText = new JTextField(10);
        houseNumberOrName = new JLabel("House Number Or Name:",JLabel.RIGHT);
        houseNumberOrNameText = new JTextField(10);
        street = new JLabel("Street:",JLabel.RIGHT);
        streetText = new JTextField(10);
        town = new JLabel("Town:", JLabel.RIGHT);
        townText = new JTextField(10);
        county = new JLabel("County:",JLabel.RIGHT);
        countyText = new JTextField(10);
        postcode = new JLabel("Postcode:",JLabel.RIGHT);
        postcodeText = new JTextField(10);
        createCustomer = new JButton("Create a Customer");
        
        
        createCustomerPanel.add(customerID);
        createCustomerPanel.add(customerIDText);
        createCustomerPanel.add(firstName);
        createCustomerPanel.add(firstNameText);
        createCustomerPanel.add(familyName);
        createCustomerPanel.add(familyNameText);
        createCustomerPanel.add(dateOfBirth);
        createCustomerPanel.add(dateOfBirthText);
        createCustomerPanel.add(houseNumberOrName);
        createCustomerPanel.add(houseNumberOrNameText);
        createCustomerPanel.add(street);
        createCustomerPanel.add(streetText);
        createCustomerPanel.add(town);
        createCustomerPanel.add(townText);
        createCustomerPanel.add(county);
        createCustomerPanel.add(countyText);
        createCustomerPanel.add(postcode);
        createCustomerPanel.add(postcodeText);
        createCustomerPanel.add(createCustomer);
        
        
        
    }
    
    private void addActionListeners()
    {
       
        createCustomer.addActionListener(this);
        
    } 
    
    public void actionPerformed( ActionEvent evt)
    
    {
        String source = evt.getActionCommand();
        
        if (source.equals("Create a Customer"))
        {
            addToCustomers();
            writeToFile();
            
        }
        
        
    
    
    }
    
}



Also here is my control class


import java.util.*;
import java.io.*;


public class MenuControlCustomers
{       
      
      ArrayList<Customer> customers = null;
      CreateCustomerGUI view = null;
      MenuControlCustomers()
      {
        customers  = CustomerIO.readFromFile();
        view = new CreateCustomerGUI(this);
      }
    
      public void showView()
      {
             view.run(); 
       }
   
    public void writeToFile()
    {
        CustomerIO.writeToFile(customers);
    }
    
    
    public  void addToCustomers(String firstName, String familyName, String dateOfBirth,
    String houseNoOrName, String street,String town,String county, String postcode) 
    {
        customers.add( new Customer(firstName,familyName,dateOfBirth,
     houseNoOrName,street,town,county,postcode) );
    }
    
    public void goodbye()
    {
        System.out.println("Goodbye");
        view.setVisible(false);
        view.dispose();
    }
    
}




Is This A Good Question/Topic? 0
  • +

Replies To: Some Help needed trying to write to an array via GUI using File IO

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 09:41 AM

As your file constains an arrayList of cutomer no need to create one
end you don't need one neither in your instance variables as all your method are static
and you do nt create CustomerIO object

import java.util.*;
import java.io.*;


public class CustomerIO {

   public static ArrayList<Customer> readFromFile()
    {      
        ObjectInputStream in = null;
        ArrayList<Customer> customers = null;
        try
            {
            in = new ObjectInputStream(new
                                       BufferedInputStream(new
                                             FileInputStream("customerfile.txt")));
            
            customers  = (ArrayList<customer>) in.readObject();
            
            }
        catch( Exception e)
        {
        }
        finally { 
                try{
                in.close(); 
                   }   
               catch (Exception e) { 
            }}
        return customers;
  }
 

Was This Post Helpful? 0
  • +
  • -

#3 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 09:50 AM

sorry I didn't quite understand that?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 09:59 AM

Sorry your's will work too just that some lines and variables are useless

import java.util.*;
import java.io.*;


public class CustomerIO {

 // this ArrayList is not used so useless
 //private ArrayList < Customer > customers;



  public static ArrayList<Customer> readFromFile()
    {
        
        ObjectInputStream in = null;
        // no need to create an ArrayList here, you will override it later
        //ArrayList<Customer> customers = new ArrayList<Customer>();
        // just need a variable to hold it
        ArrayList<Customer> customers;
        try
            {
            in = new ObjectInputStream(new
                                       BufferedInputStream(new
                                             FileInputStream("customerfile.txt")));
            // will work but no need to create a readObject you can read directy
            // ArrayList<Customer> readObject = (ArrayList<Customer>)in.readObject();
	    // customers = readObject;
            customers = (ArrayList<Customer>)in.readObject();
            
            }
        catch( Exception e)
        {
        }
        finally { 
                try{
                in.close(); 
                   }   
               catch (Exception e) { 
            }}
        return customers;
  }


Was This Post Helpful? 0
  • +
  • -

#5 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 10:17 AM

View Postpbl, on 26 November 2011 - 09:59 AM, said:

Sorry your's will work too just that some lines and variables are useless

import java.util.*;
import java.io.*;


public class CustomerIO {

 // this ArrayList is not used so useless
 //private ArrayList < Customer > customers;



  public static ArrayList<Customer> readFromFile()
    {
        
        ObjectInputStream in = null;
        // no need to create an ArrayList here, you will override it later
        //ArrayList<Customer> customers = new ArrayList<Customer>();
        // just need a variable to hold it
        ArrayList<Customer> customers;
        try
            {
            in = new ObjectInputStream(new
                                       BufferedInputStream(new
                                             FileInputStream("customerfile.txt")));
            // will work but no need to create a readObject you can read directy
            // ArrayList<Customer> readObject = (ArrayList<Customer>)in.readObject();
	    // customers = readObject;
            customers = (ArrayList<Customer>)in.readObject();
            
            }
        catch( Exception e)
        {
        }
        finally { 
                try{
                in.close(); 
                   }   
               catch (Exception e) { 
            }}
        return customers;
  }




I tried using the above am still am receiving compile issues with my code, I think its the return cutsomers that's causing an error as it says "customers may not have been initialized".

Also is the above going to work as I want it to save and load the arrayList everytime the GUI is loaded so customers will be able to be searched in my system.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2776
  • View blog
  • Posts: 11,740
  • Joined: 20-September 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 01:56 PM

You need

        // just need a variable to hold it
        ArrayList<Customer> customers = null;



Don't ignore exceptions as you are doing. It can lead to very baffling bugs
Was This Post Helpful? 0
  • +
  • -

#7 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 03:15 PM

View Postg00se, on 26 November 2011 - 01:56 PM, said:

You need

        // just need a variable to hold it
        ArrayList<Customer> customers = null;



Don't ignore exceptions as you are doing. It can lead to very baffling bugs


Thanks for your reply but I'm still not having any luck it doesn't create a file and nothing is written to the arrayList, I've been trying this for hours and can't seem to figure out whats wrong.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 04:18 PM

As g00se said, put println statements in your catch clause
Was This Post Helpful? 0
  • +
  • -

#9 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 05:39 PM

View Postpbl, on 26 November 2011 - 04:18 PM, said:

As g00se said, put println statements in your catch clause


I know I have put statements in my catch clause and my program is still not creating a text file and I am still unable to write details to the file.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 05:46 PM

Just does not make sense... you are wasting our time
Modify your code as follow and tell us what it prints

 public static void writeToFile(ArrayList<Customer> customers)
    {
        ObjectOutputStream out = null;
        try
        {
            out = new ObjectOutputStream(new
            BufferedOutputStream(new
            FileOutputStream("customerfile.txt")));
            out.writeObject(customers);
            System.out.println("An ArrayList of " + customers.size() + " has been written");
        }
        catch( Exception e) {
            System.out.println("Error " + e + " encountered when writing the ArrayList");
        }
        finally { 
            try{                out.close(); }
            catch( Exception e) {}
        }
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#11 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 06:21 PM

View Postpbl, on 26 November 2011 - 05:46 PM, said:

Just does not make sense... you are wasting our time
Modify your code as follow and tell us what it prints

 public static void writeToFile(ArrayList<Customer> customers)
    {
        ObjectOutputStream out = null;
        try
        {
            out = new ObjectOutputStream(new
            BufferedOutputStream(new
            FileOutputStream("customerfile.txt")));
            out.writeObject(customers);
            System.out.println("An ArrayList of " + customers.size() + " has been written");
        }
        catch( Exception e) {
            System.out.println("Error " + e + " encountered when writing the ArrayList");
        }
        finally { 
            try{                out.close(); }
            catch( Exception e) {}
        }
    }
    
}



It doesn't do anything, what is supposed to have is when i launch my MenuControlCustomer class it loads up the GUI which is fine then when i input the relevant information into the gui textFields and click createCustomer this doesnt save the customer to the output txt file.
Was This Post Helpful? -1
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 06:37 PM

so you simply don't call your writeToFile() method and your topic title
"Some Help needed trying to write to an array via GUI using File IO"
is quite misleading :)

Do you realize you ask us, since a while, to fix a problem that does not exist ?
Pur println() statement in your code and trace what it does

When you post the code of a method that does not seem to work
Please, at least, make sure the method is called
Was This Post Helpful? 0
  • +
  • -

#13 mitchnufc  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 49
  • Joined: 31-March 11

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 06:54 PM

View Postpbl, on 26 November 2011 - 06:37 PM, said:

so you simply don't call your writeToFile() method and your topic title
"Some Help needed trying to write to an array via GUI using File IO"
is quite misleading :)

Do you realize you ask us, since a while, to fix a problem that does not exist ?
Pur println() statement in your code and trace what it does

When you post the code of a method that does not seem to work
Please, at least, make sure the method is called



I posted the code above in the original post and I also do call my writetoFile function I must have missed it off above, like I say I'm still having problem writing the day from the GUI to a textfile
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 26 November 2011 - 07:38 PM

View Postmitchnufc, on 26 November 2011 - 09:54 PM, said:

I posted the code above in the original post and I also do call my writetoFile function I must have missed it off above, like I say I'm still having problem writing the day from the GUI to a textfile

As nothing to do with GUI or not
Your writting method should work
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2776
  • View blog
  • Posts: 11,740
  • Joined: 20-September 08

Re: Some Help needed trying to write to an array via GUI using File IO

Posted 27 November 2011 - 04:38 AM

Quote

public class MenuControlCustomers {
    ArrayList<Customer> customers = null;
    CreateCustomerGUI view = null;

    MenuControlCustomers() {
        customers = CustomerIO.readFromFile();

        view = new CreateCustomerGUI(this);
    }
...
}


That's going to do nothing at all as far as displaying Customer instances is concerned afaics. You create a List<Customer> but i don't see what happens to it then ...
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »