Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

ObjectOutputStream -- Error.

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using objectoutputstream to read/write the contents of an array
to the file.

I dont get any problem if i read the file but i am encountering
the following error when i try to read from the file.

Reading from ObjectOuputStream
java.lang.ClassCastException: [LPatient;
at Ward.main(Ward.java:368)

Any help is appreciated.

Following are the two class files that i have been using.

public class Patient implements Serializable
{
private String name = null;

public String getName()
{
return name;
}

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


public class Ward
{
public static void main(String args[]) throws Exception
{

try
{
Patient[] patients = new Patient[1];
patients[0] = new Patient();

//Writing to File
patients[0].setName("Ravi");
File file = new File("myfile");
FileOutputStream fOS = new FileOutputStream (file);
ObjectOutputStream oOS = new ObjectOutputStream (fOS);
oOS.writeObject (patients);

//Reading From File
File myFile = new File("myfile");
FileInputStream fIS = new FileInputStream (myFile);
ObjectInputStream oIS = new ObjectInputStream(fIS);
patients[0] = (Patient) oIS.readObject();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You write an array, and read it as single Object.
And code-tags wouldn't make your code less readable.
You may add them now.
[ August 22, 2004: Message edited by: Stefan Wagner ]
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have added the code tags now, Could explain me why i am getting this
problem.

It would be great if you could give me the solution.

Regards
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

Thanks for you reply, I got it right now by making the following
changes



Regards
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic