Results 1 to 4 of 4
- 10-23-2011, 01:34 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Trouble with objectjava.io.notserializableException on class that is serialized
So I'm doing a simple course database program, where I have a database object that includes arraylists of differents objects, like course and educator class objects, and a gui created using breezyswing.
Now the problem is, that every time I use my course editing class, which itself works fine, and try to save afterwards the database object, I get the error
objectjava.io.notserializableException, although all the used classes should be serialized. All the other classes, which either add or delete objects to and from the arraylists don't cause this problem.
I have tried to find information all over the internet on what I'm doing wrong, but couldn't find a solution to my problem, so any help would be appreciated.
Here's the course editing class:
And here's the database class:Java Code:package tyo_8; import java.util.*; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JTextField; import BreezySwing.GBDialog; import BreezySwing.GBFrame; public class EditPrivateCourse_GUI extends GBDialog { static final long serialVersionUID = 1; /** * variable to move information from GUI. */ private PrivateCourse privateCourse; private Omaedu_database omaedu; private Classroom classroom; private Educator educator; private ArrayList<Classroom> roomlist; private ArrayList<Educator> edulist; private Iterator<Classroom> roomiter; private Iterator<Educator> eduiter; private int identification=0; private ArrayList<PrivateCourse> temp = new ArrayList<PrivateCourse>(); private Iterator<PrivateCourse> iter; /** * Buttons. */ private JButton edit = addButton("Edit", 15, 6, 1, 1); private JButton choosecourse = addButton("Open course", 2, 4, 1, 1); /** * labels */ private JLabel headLabel = addLabel("Course information", 1,3,1,1); private JLabel setcourse = addLabel("Choose course by id:", 2,2,1,1); private JLabel privateCourseIdLabel = addLabel("Course id", 3,2,1,1); private JLabel privateCourseTitleLabel = addLabel("Coursetitle", 4,2,1,1); private JLabel privateCourseTypeLabel = addLabel("Coursetype", 5,2,1,1); private JLabel privateCourseSubjectLabel = addLabel("Coursesubject", 6,2,1,1); private JLabel privateCourseContent = addLabel("Coursecontent", 7,2,1,1); private JLabel privateCoursePriceLabel = addLabel("Courseprice", 8,2,1,1); private JLabel privateCourseMaxParticipantsLabel = addLabel("Maxparticipants", 9,2,1,1); private JLabel privateCourseMinParticipantsLabel = addLabel("Minparticipants", 10,2,1,1); private JLabel privateCourseDateLabel = addLabel("Coursedate", 11,2,1,1); private JLabel privateCourseClassroomLabel = addLabel("Classroom", 12,2,1,1); private JLabel privateCoursePersonnelLabel = addLabel("Personnel", 13,2,1,1); /** * textfield */ private JTextField privateCourseIdField = addTextField("",3,3,2,1); private JTextField privateCourseTitleField = addTextField("",4,3,2,1); private JTextField privateCourseTypeField = addTextField("",5,3,2,1); private JTextField privateCourseSubjectField = addTextField("",6,3,2,1); private JTextField privateCourseContentField = addTextField("",7,3,2,1); private JTextField privateCoursePriceField = addTextField("",8,3,2,1); private JTextField privateCourseMaxParticipantsField = addTextField("",9,3,2,1); private JTextField privateCourseMinParticipantsField = addTextField("",10,3,2,1); private JTextField privateCourseDateField = addTextField("",11,3,2,1); private JTextField privateCourseClassroomField = addTextField("",12,3,2,1); private JTextField privateCoursePersonnelField = addTextField("",13,3,2,1); /** * Empty labels for */ private JLabel emptyLabel1 = addLabel(" ", 1,1,1,1); private JLabel emptyLabel2 = addLabel(" ", 5,1,1,1); private JLabel emptyLabel3 = addLabel(" ", 7,1,1,1); private JLabel emptyLabel4 = addLabel(" ", 1,5,1,1); static boolean debugger = false; /** * Creating combobox for classroom. */ private JComboBox classRoom = addComboBox(0,0,1,1); /** * Creating combobox for personnel. * */ private JComboBox personnel = addComboBox(12,4,1,1); /** * Creating combobox for course id. * */ private JComboBox id = addComboBox(13,4,1,1); /** * Creating Dialog-window * * @param w to which frame it refers * @param c is the course information that is read */ public EditPrivateCourse_GUI(GBFrame w, Omaedu_database omaEdu, PrivateCourse p) { super(w); this.omaedu = omaEdu; temp = omaedu.getPrivateCourses(); this.privateCourse = p; this.roomlist = omaedu.getRooms(); this.edulist = omaedu.getEducators(); privateCourseIdField.setEditable(false); privateCourseTitleField.setEditable(true); privateCourseTypeField.setEditable(true); privateCourseSubjectField.setEditable(true); privateCourseContentField.setEditable(true); privateCoursePriceField.setEditable(true); privateCourseMaxParticipantsField.setEditable(true); privateCourseMinParticipantsField.setEditable(true); privateCourseDateField.setEditable(true); privateCourseClassroomField.setEditable(false); privateCoursePersonnelField.setEditable(false); if (!temp.isEmpty() && temp != null && temp.get(0) != null ) { PrivateCourse help = temp.get(0); identification = help.getId(); displayData(help); } if (!roomlist.isEmpty() && roomlist != null) { roomiter = roomlist.iterator(); while (roomiter.hasNext()) { Classroom help = roomiter.next(); classRoom.addItem(help.getId()+" "+help.getLocation()); } } else { classRoom.addItem("No rooms"); } if (!edulist.isEmpty() && edulist != null) { eduiter = edulist.iterator(); while (eduiter.hasNext()) { Educator help = eduiter.next(); personnel.addItem(help.getEmpnum()+" "+help.getEmpname()); } } if (!temp.isEmpty() && temp != null) { iter = temp.iterator(); while (iter.hasNext()) { PrivateCourse help = iter.next(); id.addItem(help.getId()+" "); } } } private void displayData(PrivateCourse p){ privateCourseIdField.setText(p.getId() + ""); privateCourseTitleField.setText(p.getPrivateCourseTitle()); privateCourseTypeField.setText(p.getPrivateCourseType()); privateCourseSubjectField.setText(p.getPrivateCourseSubject()); privateCourseContentField.setText(p.getPrivateCourseContent()); privateCoursePriceField.setText(p.getPrivateCoursePrice() + ""); privateCourseMaxParticipantsField.setText(p.getPrivateCourseMaxParticipants() +""); privateCourseMinParticipantsField.setText(p.getPrivateCourseMinPraticipants() + ""); privateCourseDateField.setText(p.getPrivateCourseDate()); String classroom = Integer.toString(p.getPrivateCourseClassroom().getId()) + p.getPrivateCourseClassroom().getLocation(); privateCourseClassroomField.setText(classroom); String educator = Integer.toString(p.getPrivateCoursePersonnel().getEmpnum()); privateCoursePersonnelField.setText(educator); } public void buttonClicked(JButton button) { if (button == choosecourse) { PrivateCourse param = new PrivateCourse(0,"empty","empty","empty","empty",0.0, 0, 0,"empty",null,null); String help = (String)id.getSelectedItem(); int i = help.indexOf(" "); String ref = help.substring(0, i); identification = Integer.parseInt(ref); iter = temp.iterator(); while (iter.hasNext()) { PrivateCourse course = iter.next(); if (course.getId() == identification) { param = course; } } displayData(param); } else if (button == edit) { try { String privateCourseTitle = privateCourseTitleField.getText(); String privateCourseType = privateCourseTypeField.getText(); String privateCourseSubject = privateCourseSubjectField.getText(); String privateCourseContent = privateCourseContentField.getText(); double privateCoursePrice = Double.parseDouble(privateCoursePriceField.getText()); int privateCourseMaxParticipants = Integer.parseInt(privateCourseMaxParticipantsField.getText()); int privateCourseMinParticipants = Integer.parseInt(privateCourseMinParticipantsField.getText()); String privateCourseDate = privateCourseDateField.getText(); classroom = new Classroom(0,"empty",0,"empty"); if (!roomlist.isEmpty() && roomlist != null) { String help = (String)classRoom.getSelectedItem(); int i = help.indexOf(" "); String roomref = help.substring(0, i); int roomid = Integer.parseInt(roomref); roomiter = roomlist.iterator(); while (roomiter.hasNext()) { Classroom temp = roomiter.next(); if (temp.getId() == roomid) { classroom = temp; } } } educator = new Educator(0,"empty","empty","empty","empty","empty"); String help2 = (String)personnel.getSelectedItem(); int j = help2.indexOf(" "); String eduref = help2.substring(0, j); int eduid = Integer.parseInt(eduref); eduiter = edulist.iterator(); while (eduiter.hasNext()) { Educator temp = eduiter.next(); if (temp.getEmpnum() == eduid) { educator = temp; } } privateCourse.setId(identification); privateCourse.setPrivateCourseTitle(privateCourseTitle); privateCourse.setPrivateCourseType(privateCourseType); privateCourse.setPrivateCourseSubject(privateCourseSubject); privateCourse.setPrivateCourseContent(privateCourseContent); privateCourse.setPrivateCoursePrice(privateCoursePrice); privateCourse.setPrivateCoursemaxParticipants(privateCourseMaxParticipants); privateCourse.setPrivateCourseMinParticipants(privateCourseMinParticipants); privateCourse.setPrivateCourseDate(privateCourseDate); privateCourse.setPrivateCourseClassroom(classroom); privateCourse.setPrivateCoursePersonnel(educator); omaedu.editPrivateCourse(privateCourse); dispose(); } catch (Exception e) { System.out.println(e); } } } }
And lastly the main program that saves and loads the database:Java Code:package tyo_8; import java.util.*; import java.io.*; public class Omaedu_database implements Serializable{ private String title; private Omaedu_database omaedu; private ArrayList<Educator> educatorList; private ArrayList<Classroom> roomList; private ArrayList<PrivateCourse> privateCourseList; private ArrayList<BusinessCourse> businessCourseList; private ArrayList<Customer> customerList; private Iterator<Customer> customeriter; private Iterator<Educator> eduiter; private Iterator<PrivateCourse> privatecourseiter; private Iterator<BusinessCourse> businesscourseiter; private Iterator<Classroom> roomiter; public Omaedu_database(String a) { this.title=a; this.educatorList= new ArrayList<Educator>(); this.roomList=new ArrayList<Classroom>(); this.privateCourseList=new ArrayList<PrivateCourse>(); this.businessCourseList=new ArrayList<BusinessCourse>(); this.customerList=new ArrayList<Customer>(); } public void addToEducators(Educator tolist) { educatorList.add(tolist); } public void removeEducators(Educator remove) { System.out.print("inside method" + remove.getEmpnum()); if(educatorList.remove(remove)) { System.out.println("ok!"); } } public void addToRooms(Classroom tolist) { roomList.add(tolist); } public void addToPrivateCourses(PrivateCourse tolist) { if (tolist.getId() > 0) { privateCourseList.add(tolist); System.out.println("Lisäys ok"); } } public void addToBusinessCourses(BusinessCourse tolist) { businessCourseList.add(tolist); } public void addToCustomers(Customer tolist) { customerList.add(tolist); } public ArrayList<Educator> getEducators() { return educatorList; } public ArrayList<Customer> getCustomers( ) { return customerList; } public ArrayList<Classroom> getRooms( ) { return roomList; } public ArrayList<PrivateCourse> getPrivateCourses() { return privateCourseList; } public ArrayList<BusinessCourse> getBusinessCourses() { return businessCourseList; } public Classroom getClassroom(int id) { Classroom retClassroom = null; Classroom tempClassroom; roomiter = this.roomList.iterator(); while (roomiter.hasNext() && retClassroom == null) { tempClassroom = roomiter.next(); if (tempClassroom.getId()==id) { retClassroom = tempClassroom; } } return retClassroom; } public Educator getEducator(int id) { Educator retEducator = null; Educator tempEducator; eduiter = this.educatorList.iterator(); while (eduiter.hasNext() && retEducator == null) { tempEducator = eduiter.next(); if (tempEducator.getEmpnum()==id) { retEducator = tempEducator; } } return retEducator; } public void removeClassroom(Classroom remove) { roomList.remove(remove); } public Customer getCustomer(String id) { Customer retCustomer = null; Customer tempCustomer; customeriter = this.customerList.iterator(); while (customeriter.hasNext() && retCustomer == null) { tempCustomer = customeriter.next(); if (tempCustomer.getId().equals(id)) { retCustomer = tempCustomer; } } return retCustomer; } public void removeCustomer(Customer remove) { customerList.remove(remove); } public PrivateCourse getPrivateCourse(int id) { PrivateCourse retPrivateCourse = null; PrivateCourse tempPrivateCourse; privatecourseiter = this.privateCourseList.iterator(); while (privatecourseiter.hasNext() && retPrivateCourse == null) { tempPrivateCourse = privatecourseiter.next(); if (tempPrivateCourse.getId() == (id)) { retPrivateCourse = tempPrivateCourse; } } return retPrivateCourse; } public void removePrivateCourse(PrivateCourse remove) { privateCourseList.remove(remove); } public void editPrivateCourse(PrivateCourse edit) { int tempId = edit.getId(); PrivateCourse temp; privatecourseiter = this.privateCourseList.iterator(); while (privatecourseiter.hasNext()) { temp = privatecourseiter.next(); if (temp.getId() == (tempId)) { int i = this.privateCourseList.indexOf(temp); this.privateCourseList.set(i, edit); System.out.println("Edit successful"); } } } public BusinessCourse getBusinessCourse(int id) { BusinessCourse retBusinessCourse = null; BusinessCourse tempBusinessCourse; businesscourseiter = this.businessCourseList.iterator(); while (businesscourseiter.hasNext() && retBusinessCourse == null) { tempBusinessCourse = businesscourseiter.next(); if (tempBusinessCourse.getId() == (id)) { retBusinessCourse = tempBusinessCourse; } } return retBusinessCourse; } public void removeBusinessCourse(BusinessCourse remove) { businessCourseList.remove(remove); } public void editBusinessCourse(BusinessCourse edit) { int tempId = edit.getId(); BusinessCourse temp; businesscourseiter = this.businessCourseList.iterator(); while (businesscourseiter.hasNext()) { temp = businesscourseiter.next(); if (temp.getId() == (tempId)) { int i = this.businessCourseList.indexOf(temp); this.businessCourseList.set(i, edit); } } } public Educator getNext(int count) { if ((this.educatorList != null) && (count >= 0) && (count < educatorList.size())){ return this.educatorList.get(count); } else { return null; } } }
Java Code:package tyo_8; import java.io.*; /** * The program that is started.<br> * Takes care of reading, creating and saving the database as well as exiting the application.<br> * * @author Sini Haapanen and Anniina Torniainen */ public class Program implements Serializable { /** * Creates a database for OmaEdu and reads the file if it exists<br> * * @param name the name of the company * @return returns the database object */ private static Omaedu_database inFile(String name){ Omaedu_database omaedu = new Omaedu_database(name); try { FileInputStream inFile = new FileInputStream("Omaedu_database.obj"); ObjectInputStream in = new ObjectInputStream(inFile); omaedu = (Omaedu_database)in.readObject(); } catch (Exception e) { //catches an exception System.out.println("File not found, creating new database!"); } return omaedu; } /** * Writes the OmaEdu-database object to file<br> * * @param omaedu -the object to be written in file */ public static void toFile(Omaedu_database omaedu){ try { FileOutputStream outFile = new FileOutputStream("Omaedu_database.obj"); ObjectOutputStream out = new ObjectOutputStream(outFile); out.writeObject(omaedu); out.flush(); out.close(); } catch (Exception e) { // catches an exception System.out.println("An error occurred while writing the object" + e); } } /** * Quitting process<br> * * @param omaedu -the object to be written in file */ public static void exitProgram(Omaedu_database omaedu){ toFile(omaedu); // writes the omaedu- object to file System.exit(0); // exiting the program } /** * @param args Starting the program */ public static void main(String[] args) { //Creates a new Secretary_GUI window with the read database object Secretary_GUI window = new Secretary_GUI(inFile("OmaEdu")); } }
-
Re: Trouble with objectjava.io.notserializableException on class that is serialized
Can you show your actual error message?
Also, why does your Omaedu_database class contain an Omaedu_database instance variable? Thankfully it's never initialized, but it does confuse me.
- 10-23-2011, 05:29 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Trouble with objectjava.io.notserializableException on class that is serialized
- 10-23-2011, 08:18 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Trouble with objectjava.io.notserializableException on class that is serialized
Fubarable, thanks for looking at my problem. And dowhile, sorry for crossposting, I just thought there would be a greater chance that someone replies, if I post on two different sites.
I'm using eclipse, and this is how the error looks like: An error occurred while writing the objectjava.io.NotSerializableException: java.util.ArrayList$Itr
If I don't handle the exception, this is how it looks:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
at tyo_8.Program.toFile(Program.java:42)
at tyo_8.Secretary_GUI.menuItemSelected(Secretary_GUI .java:80)
at BreezySwing.GBFrameMenuListener.actionPerformed(GB Frame.java:533)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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(Unk nown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(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(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Similar Threads
-
Getting exception is java.io.NotSerializableException. Please help
By Chinnu55 in forum Advanced JavaReplies: 2Last Post: 08-21-2011, 08:45 AM -
Serialized Java Obj testing
By akokane in forum New To JavaReplies: 0Last Post: 02-02-2011, 06:38 AM -
trouble with a class
By Alexander Montero in forum New To JavaReplies: 2Last Post: 06-17-2009, 10:01 PM -
FileOutputStream gets NotSerializableException
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 09:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks