में भाग लेने पर क्लासकास्ट अपवाद जब मैं एक छोटी परियोजना करने की कोशिश कर रहा हूं जिसके लिए एक अटैचमेंट ऑब्जेक्टऑटपुटस्ट्रीम की आवश्यकता है। मैं कुछ समाधानों से गुज़र चुका हूं और मुझे this मिला है, यह पहली बार मेरी समस्या का समाधान करना प्रतीत होता था। लेकिन मेरे प्रोजेक्ट के आगे के विकास पर मुझे अप्रत्याशित अपवाद मिलना शुरू हो गया। निम्नलिखित मेरी कक्षाएं हैं।ऑब्जेक्ट आउटपुटस्ट्रीम
public class PPAccount implements Serializable
{
private Profile profile;
private String email;
private float accountBal;
private boolean isActivated;
private String activationCode;
private ArrayList<Transaction> transactions;
//a few functions
}
public class PPRestrictedAccount extends PPAccount {
private String parentEmail;
private float withdrawLimit;
//a few functions
}
public class PPBusinessAccount extends PPAccount {
private ArrayList <PPRestrictedAccount> accountOperators;
//a few functions
}
public class PPStudentAccount extends PPAccount {
private String parentEmail;
//a few functions
}
मैं क्या देखा है this मैं ObjectOutputStream अधिरोहित और इसका इस्तेमाल जब मैं फाइल करने के लिए वस्तुओं जोड़कर रहा है का उपयोग कर, है। लेकिन क्या होता है यदि मैं लिखता हूं:
PPBusinessAccount
पहले, किसी भी समय दोहराएं ... फिर PPAccount
लिखें सब ठीक है। PPAccount
पहले, दोहराना .... फिर PPBusinessAccount
लिखें, फिर PPAccount
लिखें, यह अच्छी तरह से लिखता है लेकिन पढ़ने के दौरान मुझे ClassCastException
मिलता है।
कक्षाओं से बचने के लिए ऑब्जेक्ट्स पढ़ने और उन्हें सीधे Object
कक्षा के उदाहरण में संग्रहीत करने की कोशिश की गई है लेकिन अभी भी readObject()
ClassCastException
फेंकता है।
मैंने अपने परिदृश्य का वर्णन करने के लिए सबसे अच्छा प्रयास किया, बताओ कि आपको कुछ भी नहीं मिला है। ये क्यों हो रहा है?? क्या इसे हेडर के साथ कुछ करने के लिए मिला है जो यह पहली बार लिख रहा है ?? बेस क्लास हेडर की लाइनों के साथ बाल वर्ग का समर्थन नहीं कर सकता ?? चारों ओर मोड़ क्या है?
मैं इस तरह डाली कर रहा हूँ:
Object o = ois.readObject(); //Surprisingly exception is raised here (line:50 in DataStore)
PPAccount ppa = (PPAccount)o;
स्टैक ट्रेस
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:50)
at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)
Exception in thread "main" java.lang.NullPointerException
at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:66)
at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)
lookUpAccount
धारा से पढ़ता है, जबकि writeAccount
, धारा के लिए लिखते हैं यहाँ कोड है:
public static PPAccount lookupAccount(String email) throws IOException, ClassNotFoundException
{
PPAccount account = null; //initialize it after reading from file
// write code to open the files, read
PPAccount foundAccount=null;
ObjectInputStream ois=null;
FileInputStream fis=null;
File ff = new File(PPConstants.AllAccountDetails);
if(!ff.exists())
{
//System.out.println("Required file not found");
return null;
}
try
{
fis=new FileInputStream(PPConstants.AllAccountDetails);
ois = new ObjectInputStream(fis);
while(fis.available()>0 && foundAccount==null)
{
//Object o=null;
PPAccount ppa=null;
try
{
ppa = (PPAccount)ois.readObject();
if(ppa==null)
return null;
System.out.println(ppa);
}
catch(ClassCastException cce)
{
System.out.println("Class cast exception "+cce.getCause());
cce.printStackTrace();
}
if(email.equals(ppa.getEmail()))
{
foundAccount=ppa;
break;
}
if(ppa instanceof PPBusinessAccount)
{
PPBusinessAccount ppba = (PPBusinessAccount)ppa;
ArrayList<PPRestrictedAccount> alist=ppba.getAccountOperators();
if(alist==null)
continue;
Iterator<PPRestrictedAccount> it = alist.iterator();
while(it.hasNext())
{
PPRestrictedAccount ppr=(PPRestrictedAccount) it.next();
System.out.println(ppr);
if(email.equals(ppr.getEmail()))
{
foundAccount = ppr;
break;
}
}//iterators while loop
}//if it is a businessAccount
}//outer while
}//try
finally
{
if(ois!=null)
ois.close();
if(fis!=null)
fis.close();
}
return foundAccount;
}
public static void writeAccount(PPAccount account,Boolean append) throws IOException, ClassNotFoundException, DuplicateAccountException
{
ObjectOutputStream oos=null;
FileOutputStream fos=null;
try
{
if(!append)
{
fos= new FileOutputStream(PPConstants.AllAccountDetails);
oos = new ObjectOutputStream(fos);
//System.out.println("Not Appending");
oos.writeObject(account);
}
else
{
File ff = new File(PPConstants.AllAccountDetails);
if(!ff.exists())
{
System.out.println("Required file not found");
return;
}
PPAccount aa=lookupAccount(account.getEmail());
if(aa!=null)
throw new DuplicateAccountException("An Account already exits with this email-ID");
oos = new AppendingObjectOutputStream(new FileOutputStream(PPConstants.AllAccountDetails,append));
oos.writeObject(account);
}
}
finally
{
if(oos!=null)
oos.close();
if(fos!=null)
fos.close();
}
}
क्या आप अपवाद ट्रेस भी पोस्ट कर सकते हैं? यह गलत क्या है इसका एक स्पष्ट संकेत देता है ... – SiB
क्या आप स्ट्रीम में कुछ और लिख रहे हैं? उदाहरण के लिए, writeObject() के अलावा किसी भी विधि के साथ? – EJP
@EJP नहीं, मैं स्ट्रीम के लिए कुछ और नहीं लिख रहा हूं, जो 'writeObject()' है जो कि इन वर्गों में से किसी एक को लिखने वाला एकमात्र तरीका है। – sasidhar