• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem Using vector

 
thoms regular
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the problem that when i write object to
to ObjectOutput i have goted serialization execption
if the oject is string type it is done correctly
but when i used object of type "Add" i have goted a run time execption
i donot khnow what is going on
please
help
thank!






import java.util.*;
import java.io.*;
class Add
{
String s;
String s1;
public Add(String m,String mn)
{
s=m+"\n";
s1=mn+"\n";
}
public String toString()
{
return s+s1;
} }
class Showingvector
{
public static void main(String sm[]) throws Exception
{

FileOutputStream f=new FileOutputStream("jeay.txt");
ObjectOutputStream dt=new ObjectOutputStream(f);
Vector v=new Vector();
Add a=new Add("NABI BUX BALOCH","JEAY SINDHU DESH ");
String as=a.toString();
dt.writeObject(new String("JEAY SINDHU DESH "));
dt.writeObject(as);
dt.writeObject(new Add("NABI BUX","PAKISTAN"));// EXCEPTION GOTED HERE WHY THIS IS HAPPEN
v.addElement(new Add("NABI BUX BALOCH","JEAY SINDHU DESH "));
v.addElement(new Add("NABI BUX BALOCH","JEAY SINDHU DESH "));
v.addElement(new Add("NABI BUX BALOCH","JEAY SINDHU DESH "));

Enumeration vn=v.elements();

System.out.println("Element In vector");
while(vn.hasMoreElements())
{

System.out.println(" "+vn.nextElement());
}}}
 
Manish Malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading about Serializable interface is recommended.

In a nutshell, the class 'Add' should implement the above interface for the programmer to be able to serialize its state. You wouldn't need to add another method to the class; declaring that the class 'Add' implements the Serializable interface should work. (hint: see the usage of Java keyword 'implements')
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!