now I need some help with Serializing my arraylist. Now I have managed to get the serializing aspect working (I think at least), now my problem lays with de-serializing the object. I am making a small address book program. I have a comboBox that stores the addresses with three textboxes above where the user enters: name, address, and phone number. For testing purposes I then have a save and load button. The save button saves the contacts, and the load button loads the previous sessions contacts. Now everything but the de-serializing works and I would like to know how to proceed with it.
My code is bellow:
import java.awt.EventQueue; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.DefaultComboBoxModel; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; public class Address_Book { private JFrame frame; private JTextField newName; private JTextField newAddress; private JTextField newPhoneAddress; ArrayList<Book> test = new ArrayList<Book>(); ArrayList<Book> array = new ArrayList<Book>(); File addBook = new File("addBook.txt"); final JComboBox<String> comboBox = new JComboBox<String>(); final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Address_Book window = new Address_Book(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Address_Book() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); newName = new JTextField(); newName.setBounds(10, 29, 107, 20); frame.getContentPane().add(newName); newName.setColumns(10); JLabel lbl1 = new JLabel("Enter New Name:"); lbl1.setBounds(10, 11, 107, 14); frame.getContentPane().add(lbl1); JLabel lbl2 = new JLabel("Enter New Address:"); lbl2.setBounds(136, 11, 130, 14); frame.getContentPane().add(lbl2); newAddress = new JTextField(); newAddress.setColumns(10); newAddress.setBounds(136, 29, 107, 20); frame.getContentPane().add(newAddress); newPhoneAddress = new JTextField(); newPhoneAddress.setColumns(10); newPhoneAddress.setBounds(262, 29, 162, 20); frame.getContentPane().add(newPhoneAddress); JLabel lbl3 = new JLabel("Enter New Phone number:"); lbl3.setBounds(262, 11, 162, 14); frame.getContentPane().add(lbl3); JButton btnAddNewContact = new JButton("Add new contact"); btnAddNewContact.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { test.add((new Book(newName.getText(), newAddress.getText(), newPhoneAddress.getText()))); //mergesort.mergesort(test, 0, test.size() - 1); model.removeAllElements(); for(int i=0; i < test.size();i++){ model.addElement(test.get(i).getContact()); } comboBox.setModel(model); newName.setText(""); newAddress.setText(""); newPhoneAddress.setText(""); } }); btnAddNewContact.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnAddNewContact.setBounds(10, 53, 414, 23); frame.getContentPane().add(btnAddNewContact); JLabel lbl4 = new JLabel("Current Contacts:"); lbl4.setBounds(10, 87, 107, 14); frame.getContentPane().add(lbl4); frame.getContentPane().add(comboBox); comboBox.setModel(model); comboBox.setBounds(10, 101, 414, 20); comboBox.setSelectedIndex(test.size()-1); JButton btnLoad = new JButton("Load"); btnLoad.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { try { /* Read objects */ FileInputStream in = new FileInputStream(addBook); ObjectInputStream readIn = new ObjectInputStream(in); array = (ArrayList<Book>) readIn.readObject(); readIn.close(); for(int i=0; i < array.size();i++){ model.addElement(array.get(i).getContact()); } comboBox.setModel(model); }catch(Exception e1){ e1.printStackTrace(); } } }); btnLoad.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnLoad.setBounds(10, 132, 89, 23); frame.getContentPane().add(btnLoad); JButton btnSave = new JButton("Save"); btnSave.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { /* write objects */ try{ FileOutputStream out = new FileOutputStream(addBook); ObjectOutputStream writeAdd = new ObjectOutputStream(out); writeAdd.writeObject(test); writeAdd.close(); }catch(Exception e){ } } }); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); btnSave.setBounds(109, 132, 89, 23); frame.getContentPane().add(btnSave); } }
Here is my object:
public class Book implements Comparable { private String flName, Address, pNumber; public Book(String Name, String address, String phoneNumber ){ setFlName(Name); setAddress(address); setpNumber(phoneNumber); } public String getpNumber() { return pNumber; } public void setpNumber(String pNumber) { this.pNumber = pNumber; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public String getFlName() { return flName; } public void setFlName(String flName) { this.flName = flName; } public String getContact() { return flName + ", " + Address + ", " + pNumber; } public int compareTo(Object c) { Book testBook = (Book)c; if (testBook.getFlName().compareTo(this.getFlName()) < 0){ return(-1); }else if(testBook.getFlName().compareTo(this.getFlName()) == 0){ return(0); }else{ return(1); } } }
This next splotch of code is in my Address_Book class, the first code I gave you, this is just to make it easier to find where I am loading it from.
JButton btnLoad = new JButton("Load"); btnLoad.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { try { /* Read objects */ FileInputStream in = new FileInputStream(addBook); ObjectInputStream readIn = new ObjectInputStream(in); array = (ArrayList<Book>) readIn.readObject(); readIn.close(); for(int i=0; i < array.size();i++){ model.addElement(array.get(i).getContact()); } comboBox.setModel(model); }catch(Exception e1){ e1.printStackTrace(); } } }); btnLoad.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnLoad.setBounds(10, 132, 89, 23); frame.getContentPane().add(btnLoad);
Thank you for your time, if you have any questions don’t hesitate to ask. 🙂
Advertisement
Answer
- Your Book class doesn’t implement Serializable. Fix this as this is the cause for your exception and is thus the primary reason your current attempt to serialize the
ArrayList<Book>
is failing. - Google for and read a serialization tutorial.
- Again, don’t use MouseListeners on JButtons when you should be using an ActionListener. This isn’t causing your problem but will cause future problems if not fixed. Please read the Java tutorials on how to use JButtons as it’s all very explained there.
- Again, when you try to implement a new complex functionality to an already complex program, do it in isolation of the complex program first so as to isolate and fix any problems before adding it to the greater program. This is doubly true if your new code causes an error.