Serialization problem
807591May 4 2007 — edited Feb 29 2008I have a problem with some serialization. I am not so new to Java, but not experienced enough to figure out some stuff. I have a Email class that works the same way as this code for Users, but it works fine.
The problem is this;
object written...
User class louis.tutorial.Users
Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to louis.tutorial.Users
at louis.tutorial.Users.read(Users.java:121)
at louis.tutorial.Users.main(Users.java:139)
Yes, this looks exactly like a cast problem, except that it is the same class. It saves just fine. Reading is another story.
Here is the code:
package louis.tutorial;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import javax.swing.JPasswordField;
import sun.security.util.Password;
/**
* Place class description here
*
* @version 1.0
* @author llakser
*
*/
public class Users implements Serializable {
private String UserName = new String("Louis Lakser");
private transient JPasswordField password = new JPasswordField("flew163");
//used to establish a default connection to the DDCNetworkClient at logon
private boolean CONNECT = false;
FileOutput Runtime = null;
/**
* testing default constructor
*/
public Users() {
}
/**
* Overridden constructor takes is a Username and a password I must save
* this information to the EEC_Applet_Configuration.config file
*
* @param username
* @param password
* @param connect
*/
public Users(String username, JPasswordField password, boolean connect) { // JPasswordField
// password,
this.UserName = username;
this.password = password;
this.CONNECT = connect;
Date today = new Date();
Runtime = new FileOutput("RunTime", "Inside = " + Users.class + " the date = "
+ today + "New user added UserName = " + UserName
+ " Password = " + password + " Connection at logon = " + CONNECT
+ "\n");
Runtime.close();
}
/**
* saves itself to a file
* @throws IOException
*/
public void save() throws IOException {
// Use a FileOutputStream to send data to a file
// called myobject.data.
FileOutputStream f_out = null;
f_out = new FileOutputStream("Users.data");
// Use an ObjectOutputStream to send object data to the
// FileOutputStream for writing to disk.
ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
// Pass our object to the ObjectOutputStream's
// writeObject() method to cause it to be written out
// to disk.
obj_out.writeObject(Users.class);
obj_out.close();
f_out.close();
}
/**
* @throws IOException supposed to read itself from the same file
* @throws ClassNotFoundException
*/
public void read() throws IOException, ClassNotFoundException {
// Read from disk using FileInputStream.
FileInputStream f_in = new FileInputStream("Users.data");
// Read object using ObjectInputStream.
ObjectInputStream obj_in = new ObjectInputStream(f_in);
Users user = null;
// Read an object.
// Object obj = null;
System.out.println("User " + Users.class);
user = (Users) obj_in.readObject(); // <- this is the error spot!!!!!
System.out.println(" made it ");// + user.UserName + "\n" +
// user.getPassword() + "\n" +
// user.isCONNECT());
}
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException,
ClassNotFoundException {
JPasswordField passwor = new JPasswordField("flew");
Users user = new Users("louis Lakser", passwor, true);// passwor,
user.save();
System.out.println("object written...");
user.read();
}
/**
* @return the password
*/
public JPasswordField getPassword() {
return password;
}
/**
* @param password
* the password to set
*/
public void setPassword(JPasswordField password) {
this.password = password;
}
/**
* @return the userName
*/
public String getUserName() {
return UserName;
}
/**
* @param userName
* the userName to set
*/
public void setUserName(String userName) {
UserName = userName;
}
/**
* @return the cONNECT
*/
public boolean isCONNECT() {
return CONNECT;
}
/**
* @param connect
* the cONNECT to set
*/
public void setCONNECT(boolean connect) {
CONNECT = connect;
}
}
Someone please help me...