Hi folks,
Small bit of an issue trying to use ObjectInputStream and ObjectOutputStream. I'm tring to write an object of type reminder to a file so it can be opend later. not sure where im going wrong but somewhere. Not used to posting questions like this on forums but if any more info is needed reply to this and ill re post.

It keeps catching the classNotFound exception. If anyone has any ideas why this might be happening I'd greatly apppericiate it. I am having the same issue when writing and classes arnt that different so hopefully same problem blocking both classes and sort 1 sort the other.
Thanks

private class LoadListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Container content = getContentPane();
try {
FileInputStream instream = new FileInputStream("Data.bin");
try {

ObjectInputStream in = new ObjectInputStream(instream);
reminder = (controller) in.readObject();
in.close();
} finally {
instream.close();
// switch to view GUI
content.removeAll();
content.add(viewReminder);
currentIndex=0;


}

} catch (ClassNotFoundException notfound) {
lName.setText("Illegal Information in address book file ...");
} catch (OptionalDataException data) {
lName.setText("Primitive data in address book file ...");
} catch (InvalidClassException invalid) {
lName.setText("Something is wrong with a class serialization");
} catch (IOException io) {
lName.setText("Problems accessing address book file ...");
} catch (ClassCastException cast) {
lName.setText("Illegal objects in address book file ...");
}
}
}

Add a stack trace to the catch block for ClassNotFoundException

} catch (ClassNotFoundException notfound) {
notfound.printStackTrace();
lName.setText("Illegal Information in address book file ...");
}

If your code above compiles and does not complain "symbol" not found about the "controller" class, then it's probably a class within "controller" that is causing your problem. Not sure where that "controller" class comes from, but make sure that it's dependencies are on your class path.

Cheers for getting back to me. Tried you suggestion and no joy. controller is a seperate class that im using to hold several methods. I've been playing with the code for a while and it's now being caught by IOException. full code for writing and reading is included below.

private class LoadListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				Container content = getContentPane();
				try {
					FileInputStream instream = new FileInputStream("Data.bin");
					try {

						ObjectInputStream in = new ObjectInputStream(instream);
						reminder = (controller) in.readObject();
						in.close();
					} finally {
						instream.close();
						// no matter what, switch to view GUI
						content.removeAll();
						content.add(viewReminder);
						currentIndex=0;
						
						
					}
					/* alternative approach using AddressBook method
					 * contacts.loadAddresses("addresses.bin");
					 */
				} catch (ClassNotFoundException notfound) {
					lName.setText("Illegal Information in address book file ...");
				} catch (OptionalDataException data) {
					lName.setText("Primitive data in address book file ...");
				} catch (InvalidClassException invalid) {
					lName.setText("Something is wrong with a class serialization");
				} catch (IOException io) {
					lName.setText("Problems accessing address book file ...");
				} catch (ClassCastException cast) {
					lName.setText("Illegal objects in address book file ...");
				}
			}
		}

		private class SaveListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				Container content = getContentPane();
				try {
					FileOutputStream outstream = new FileOutputStream("Data.bin");
					try {
						ObjectOutputStream out = new ObjectOutputStream(outstream);
						for(int i = 0; i < reminder.notices.size();i++)
						{
							Object r = reminder.notices.get(i);
							out.writeObject(r);
						}
						
					} finally {
						outstream.close();
						// no matter what, switch to view GUI
						content.removeAll();
						content.add(viewReminder);
						
					}
					/* alternative approach using AddressBook method
					 * contacts.saveAddresses("addresses.bin");
					 */
					
				} catch (IOException io) {

lName.setText("Problems accessing address book file ...");
				}
			}
	}

You need to print the stack trace in those catch blocks. You are throwing away all of the info that could help you track down the problem by just catching them and setting some generic text info. Add <exceptionVariable>.printStackTrace(); to each block.

Hi once again thanks for getting back to me. Put in those StackTrace's and heres the errors that i'v received. It's just nonsence to me but maybe it'll make more sence to you.

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: reminder
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at gui$LoadListener.actionPerformed(gui.java:302)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.NotSerializableException: reminder
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at gui$SaveListener.actionPerformed(gui.java:341)
... 27 more

Hi,
Heres the reminder class code

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;


public class reminder {
	private String name;
	private String details;
	private String date;
	private String todayDate;

	public reminder(){
		name = new String();
		details = new String();
		date = new String();
		
		
	}

	public reminder(String nam, String detail, String dat){
		name = nam;
		details = detail;
		date = dat;
		
	}

	public String getName(){
		return name;
	}
	
	public void setName(String nam) {
		name = nam;
	}
	
	public String getDetails(){
		return details;
	}
	
	public void setDetails(String detail){
		details = detail;
	}

	public String getDate(){
		return date;
	}
	
	public void setDate(String dat){
		date = dat;
	}
	
}

I could kick myself. you've no idea the time iv spent trawling through the code trying to find the error. Thanks a million dude. saved my life

This article has been dead for over six months. Start a new discussion instead.