Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 06:22 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
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.

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; } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2007, 06:31 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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!

Code:
Person temp = (Person) vector.elementAt(i);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String) baltimore New To Java 2 09-18-2008 09:30 AM
Vector problem Ace_Of_John New To Java 1 01-27-2008 10:53 PM
vector problem mambo_jumbo New To Java 1 11-18-2007 12:44 AM
java.lang.OutOfMemoryError with java vector mary New To Java 3 08-03-2007 12:55 PM
ClassCastException Ed New To Java 2 07-04-2007 07:26 AM


All times are GMT +3. The time now is 10:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org