Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble with objectjava.io.notserializableException on class that is serialized

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with objectjava.io.notserializableException on class that is serialized

    Hi, I have a simple course database program, that consists of an object including arraylists, that in turn include objects of different classes, like course and educator objects, and that uses breezyswing for gui.
    The problem is, when I use my course editing class, which itself works withouth fail, and try to save the database object afterwards, I get the error objectjava.io.notserializableException, like the object would have turned into a non-serialized object. All the classes should though be serialized, and all the courses adding or deleting arraylist objects don't cause this problem.
    So my question is, what am I doing wrong? I haven't been able to find a solution despite searching.

    I'll put the most important classes here, excluding the main menu class, which is just a gui window.
    Here's the course editing class:
    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 and database.
    	 */
    	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;
    	/**
    	 * Button to add privateCourse-object information.
    	 */
    	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);
    	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;
    				}
    			}
     
     
    			System.out.println("The id is: " + identification);
    			privateCourse.setId(identification);
    			System.out.println("id toimii");
    			privateCourse.setPrivateCourseTitle(privateCourseTitle);
    			System.out.println("titteli toimii");
    			privateCourse.setPrivateCourseType(privateCourseType);
    			System.out.println("type toimii");
    			privateCourse.setPrivateCourseSubject(privateCourseSubject);
    			System.out.println("sub toimii");
    			privateCourse.setPrivateCourseContent(privateCourseContent);
    			System.out.println("content toimii");
    			privateCourse.setPrivateCoursePrice(privateCoursePrice);
    			System.out.println("price toimii");
    			privateCourse.setPrivateCoursemaxParticipants(privateCourseMaxParticipants);
    			System.out.println("max toimii");
    			privateCourse.setPrivateCourseMinParticipants(privateCourseMinParticipants);
    			System.out.println("min toimii");
    			privateCourse.setPrivateCourseDate(privateCourseDate);
    			System.out.println("date toimii");
    			privateCourse.setPrivateCourseClassroom(classroom);
    			System.out.println("class toimii");
    			privateCourse.setPrivateCoursePersonnel(educator);
    			System.out.println("pers toimii");
    			omaedu.editPrivateCourse(privateCourse);
    			dispose();
    			} catch (Exception e) 
    			{ System.out.println(e); 
    			}
    		}
    	}
     
    }
    Here's the database class:
    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("tulee tänne" + 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;
    		}
    	}
    }
    Here's the main program class:
    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"));	
     
    	}	
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,234
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Trouble with objectjava.io.notserializableException on class that is serialized


  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with objectjava.io.notserializableException on class that is serialized

    Sorry for that, I though this site wasn't that connected to the other site. I just wanted to be sure I would get help, since I don't know which sites are best ones for it. Is there a way for me to delete this thread?

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,234
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Trouble with objectjava.io.notserializableException on class that is serialized

    Quote Originally Posted by Ildtris View Post
    Sorry for that, I though this site wasn't that connected to the other site. I just wanted to be sure I would get help, since I don't know which sites are best ones for it. Is there a way for me to delete this thread?
    They are only connected through the fact that they attract similar audiences, meaning some common users. One of the links I provided above clearly explains the reasons why we ask for links to other forums. Most importantly - someone could waste their valuable time answering a question that has already been answered somewhere else.

Similar Threads

  1. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  2. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  3. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 03:00 PM
  4. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM