PDA

View Full Version : Help! java.lang.ClassCastException: java.util.Vector


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;
}
}
}

Aradon
11-19-2006, 07:46 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;
}
}
}


Can you give us a line number or point out where the exception says the error is?

From here I can guess that the problem is in your createElement(). You are returning a Vector filled with a Person's name and age (to which I wonder where it's set). You then add that vector to a vector. To which you try and access this vector and cast it to a person. But it's not a person, it's a vector.

Creation

if(action.equals("Add"))
{
Vector r = new Vector();
r = createElement();
vector.addElement(r);
}

//********************************
public Vector createElement()
{
Vector t = new Vector();
Person p = new Person(name, age);
t.addElement(p.name);
t.addElement(p.age);
return t;
}


Accessing

for(int i=0; i<vector.size(); i++)
{
Person temp = (Person) vector.elementAt(i);
out.writeObject(temp);
}


You're other error (from my guess) would be here:

Person temp = (Person) in.readObject();


How is (whatever object is being read) a person? Are we to assume that whatever file is being read is indeed a person? I suppose you're assuming that the save was hit first. A very dangerous assumption.

In any case, what line is the error so that I can stop my guessing and give you a good answer ;)

ramapple
11-19-2006, 08:27 PM
Thank you, from your point of view, I agree your first assumption was my fault. The error does point to the area where I call a Person when it is a vector. From your information given, hopefully I will be able to solve all my problems. +rep!