Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Problem with vector, java.lang.ClassCastException

    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 have been puzzed by this error for hours now, and have tried many methods without success.

    Java Code:
    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;
    		}
    	}
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    In your createElement method, you are adding Vectors into your Vector. But in "Save" action, you are trying to obtain Person objects! Either add Person objects to your Vector or try to obtain Vector objects from your Vector!

    So you can't cast a Vector object to Person object. This is your mistake!

    Java Code:
    Person temp = (Person) vector.elementAt(i);

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  2. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 09:53 PM
  3. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 11:44 PM
  4. java.lang.OutOfMemoryError with java vector
    By mary in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 11:55 AM
  5. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •