4 Replies - 451 Views - Last Post: 14 July 2017 - 01:31 AM Rate Topic: -----

#1 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 07-November 16

Serialization/Deserialization - List comes back as null.

Posted 13 July 2017 - 01:25 PM

I'm supposed to serialize a list of objects. Persons in my case...
And then create the serialization file, and then deserialize
objects from that .ser file that was created during serialization.
My List comes back as null... as if there wasn't anything in the file...
What in the world am I doing wrong here?

I've also included a zipped folder in the attachments of the program.


Class to create Person Objects.
package serialization;

public class Person {

	private String name;
	private String phoneNumber;
	
	
	public Person(String name, String phoneNumber)
	{
		this.name = name;
		this.phoneNumber = phoneNumber;
	}


	public void setName(String name){
		this.name = name;
		
	}
	public String getName() {
		return name;
	}


	public void setPhoneNumber(String phone){
		this.phoneNumber = phone;
	}
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	
	@Override
	public String toString(){
		return String.format("Name: %s    Phone Number: %s", name, phoneNumber);
	}
}




My Main Class where everything is done.
package serialization;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class personSerializationTest implements Serializable {

	public static void main (String[] args){
		
		Serialize s = new Serialize();
		List<Person> myPersons = new ArrayList<Person>();

		Person p1, p2, p3, p4, p5;
		
		p1 = new Person("Heather Smith", "801-503-1069");
		p2 = new Person("Tad Jones", "801-555-2069");
		p3 = new Person("John Kennamer", "503-546-1069");
		p4 = new Person("Theresa Inglewood", "218-889-1069");
		p5 = new Person("Dan Berry", "309-544-1889");
		
		
		myPersons.add(p1);
		myPersons.add(p2);
		myPersons.add(p3);
		myPersons.add(p4);
		myPersons.add(p5);
		for(Person p: myPersons){
			System.out.println(p.toString());
		}
		
		File fileName = new File("src/serialization/serPersons.ser");
		
		s.serialize(myPersons, fileName);
		
		System.out.println("Serialization Done");
		
		
		List<Person> newPersons = s.deserialize(fileName);
		System.out.println("Deserialization is done.");
		
		System.out.println("The new List: " + newPersons);

	}
}




The Serialization class to serialize() and deserialize()
package serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

public class Serialize {
	
	public Serialize()
	{
		
	}

	public void serialize(List<Person> pList, File fileName)
	{
		try(ObjectOutputStream out = 
				new ObjectOutputStream(new FileOutputStream(fileName)))
		{
		
		}catch (IOException e) 
		{
			System.out.printf("The File was not found: %s", fileName);
			System.out.println(e.getMessage());
		
		}
	}
	
	public List<Person> deserialize(File fileName)
	{
		List<Person> pList = null; //It's set to null if no data is returned it will be null. 
		
		try(ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName)))
		{
			pList = (List<Person>) in.readObject();
		}
		catch (IOException|ClassNotFoundException e)
		{ 
			System.out.printf("A problem occured during deserialization: %s", fileName);
			System.out.println(e.getMessage());
		}
		return pList;     //This list returns null, as if nothing came from the file. 

	}
}



This is all the data that saved to my serialization.ser file... which seems really short compared to the tutorial I watched.



Is This A Good Question/Topic? 0
  • +

Replies To: Serialization/Deserialization - List comes back as null.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13395
  • View blog
  • Posts: 53,461
  • Joined: 12-June 08

Re: Serialization/Deserialization - List comes back as null.

Posted 13 July 2017 - 01:30 PM

It has been some time since I had to bop around in the java sphere, but shouldn't your Person class implement serializable as well?
Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1567
  • View blog
  • Posts: 3,520
  • Joined: 05-April 11

Re: Serialization/Deserialization - List comes back as null.

Posted 13 July 2017 - 02:49 PM

Your serialize method doesn't seem to be writing any object at all. You open an object stream, but never actually write anything to it.
Was This Post Helpful? 2
  • +
  • -

#4 Heathersmithx  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 07-November 16

Re: Serialization/Deserialization - List comes back as null.

Posted 13 July 2017 - 05:02 PM

View PostCasiOo, on 13 July 2017 - 02:49 PM, said:

Your serialize method doesn't seem to be writing any object at all. You open an object stream, but never actually write anything to it.


You were right. I didn't put out.writeObject(); in the try block, so it just didn't do anything. It's working now. Thank you!
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3422
  • View blog
  • Posts: 15,477
  • Joined: 20-September 08

Re: Serialization/Deserialization - List comes back as null.

Posted 14 July 2017 - 01:31 AM

Your naming is wrong: class names should be nouns, not verbs. They begin upper case
http://technojeeves....yle-conventions
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1