RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 317 | Replies: 7
Reply
Join Date: May 2008
Posts: 5
Reputation: neonic75 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neonic75 neonic75 is offline Offline
Newbie Poster

Issues with ObjectInputStream and ObjectOutputStream

  #1  
May 2nd, 2008
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 ...");
}
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,147
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 313
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Issues with ObjectInputStream and ObjectOutputStream

  #2  
May 2nd, 2008
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.
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: neonic75 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neonic75 neonic75 is offline Offline
Newbie Poster

Re: Issues with ObjectInputStream and ObjectOutputStream

  #3  
May 2nd, 2008
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 ...");
				}
			}
	}
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,147
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 313
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Issues with ObjectInputStream and ObjectOutputStream

  #4  
May 2nd, 2008
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.
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: neonic75 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neonic75 neonic75 is offline Offline
Newbie Poster

Re: Issues with ObjectInputStream and ObjectOutputStream

  #5  
May 2nd, 2008
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
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: neonic75 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neonic75 neonic75 is offline Offline
Newbie Poster

Re: Issues with ObjectInputStream and ObjectOutputStream

  #6  
May 2nd, 2008
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;
	}
	
}
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,147
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 313
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Issues with ObjectInputStream and ObjectOutputStream

  #7  
May 2nd, 2008
The class you are trying to read and write needs to implement Serializable. This info should help: http://java.sun.com/developer/techni...serialization/
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: neonic75 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
neonic75 neonic75 is offline Offline
Newbie Poster

Re: Issues with ObjectInputStream and ObjectOutputStream

  #8  
May 2nd, 2008
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:30 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC