ramapple
11-19-2006, 05:07 AM
Currently I am trying to save and load a number of objects from a vector, however I have had no luck due to a java.lang.ClassCastException error. I've been puzzed by this error for hours now, and have tried many methods without success. If anyone here knows the answer, please explain why. Thanks in advance.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class testing extends JFrame implements ActionListener
{
Container c2;
JButton addbutton;
JButton savebutton;
JButton openbutton;
String name;
String age;
Vector vector;
public static void main (String args[]){new testing();}
public testing()
{
vector = new Vector();
name = "Name";
age = "Age";
makeButtons();
setSize(200, 150);
setVisible(true);
}
public void makeButtons()
{
c2 = getContentPane();
c2.setLayout(new FlowLayout());
addbutton = new JButton("Add");
c2.add(addbutton);
addbutton.addActionListener(this);
savebutton = new JButton("Save");
c2.add(savebutton);
savebutton.addActionListener(this);
openbutton = new JButton("Open");
c2.add(openbutton);
openbutton.addActionListener(this);
}
public void actionPerformed(ActionEvent eh)
{
String action = eh.getActionCommand();
if(action.equals("Add"))
{
Vector r = new Vector();
r = createElement();
vector.addElement(r);
}
else if(action.equals("Save"))
{
try
{
System.out.println("Writing Object");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.tmp"));
for(int i=0; i<vector.size(); i++)
{
Person temp = (Person) vector.elementAt(i);
out.writeObject(temp);
}
out.close();
}
catch(Exception e){e.printStackTrace();}
}
else if(action.equals("Open"))
{
try
{
System.out.println("Reading Object");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.tmp"));
while(true)
{
Person temp = (Person) in.readObject();
vector.addElement(temp);
}
}
catch(Exception e ){e.printStackTrace();}
}
System.out.println(vector);
}
public Vector createElement()
{
Vector t = new Vector();
Person p = new Person(name, age);
t.addElement(p.name);
t.addElement(p.age);
return t;
}
class Person implements Serializable
{
String name;
String age;
public Person(String name, String age)
{
this.name = name;
this.age = age;
}
}
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class testing extends JFrame implements ActionListener
{
Container c2;
JButton addbutton;
JButton savebutton;
JButton openbutton;
String name;
String age;
Vector vector;
public static void main (String args[]){new testing();}
public testing()
{
vector = new Vector();
name = "Name";
age = "Age";
makeButtons();
setSize(200, 150);
setVisible(true);
}
public void makeButtons()
{
c2 = getContentPane();
c2.setLayout(new FlowLayout());
addbutton = new JButton("Add");
c2.add(addbutton);
addbutton.addActionListener(this);
savebutton = new JButton("Save");
c2.add(savebutton);
savebutton.addActionListener(this);
openbutton = new JButton("Open");
c2.add(openbutton);
openbutton.addActionListener(this);
}
public void actionPerformed(ActionEvent eh)
{
String action = eh.getActionCommand();
if(action.equals("Add"))
{
Vector r = new Vector();
r = createElement();
vector.addElement(r);
}
else if(action.equals("Save"))
{
try
{
System.out.println("Writing Object");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.tmp"));
for(int i=0; i<vector.size(); i++)
{
Person temp = (Person) vector.elementAt(i);
out.writeObject(temp);
}
out.close();
}
catch(Exception e){e.printStackTrace();}
}
else if(action.equals("Open"))
{
try
{
System.out.println("Reading Object");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.tmp"));
while(true)
{
Person temp = (Person) in.readObject();
vector.addElement(temp);
}
}
catch(Exception e ){e.printStackTrace();}
}
System.out.println(vector);
}
public Vector createElement()
{
Vector t = new Vector();
Person p = new Person(name, age);
t.addElement(p.name);
t.addElement(p.age);
return t;
}
class Person implements Serializable
{
String name;
String age;
public Person(String name, String age)
{
this.name = name;
this.age = age;
}
}
}