Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

ClassCastException problem with 2D float array serialization

Thread view: 
erenay - 25 Apr 2006 21:08 GMT
Hi, I get an "java.lang.ClassCastException: [[D" with the following
code:

FileOutputStream fout= new FileOutputStream("C:\\A.txt");
ObjectOutputStream out= new ObjectOutputStream(fout);
...
float[][] vocabArray = new float[voc_cnt][groupNum];
...
out.writeObject(vocabArray);
out.close();
...
float[][] vocabArray2 = new float[voc_cnt][groupNum];
FileInputStream fin5 = new FileInputStream("C:\\A.txt");
ObjectInputStream oin5 = new ObjectInputStream(fin5);
vocabArray2 = (float[][])(oin5.readObject()); //It gives error here
oin5.close();

Can anybody see any problem here? I would really appreciate any help.
Thomas Hawtin - 26 Apr 2006 20:11 GMT
> Hi, I get an "java.lang.ClassCastException: [[D" with the following
> code:
[quoted text clipped - 12 lines]
> vocabArray2 = (float[][])(oin5.readObject()); //It gives error here
> oin5.close();

What you have written appears to be fine. However, is that exactly what
you have tried, and are you sure that you wrote over your file?

Here's some complete code I wrote. Does that work for you?

import java.io.*;

class Save {
    public static void main(String[] args) throws Exception {
        OutputStream fileOut = new FileOutputStream("file.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        float[][] array = new float[10][20];
        out.writeObject(array);
        out.close();
    }
}
class Load {
    public static void main(String[] args) throws Exception {
        InputStream fileIn = new FileInputStream("file.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        float[][] array = (float[][])in.readObject();
        in.close();
    }
}

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

erenay - 29 Apr 2006 15:05 GMT
Thank you Thomas, I've created the serialized array's File again and it
worked fine.
Oliver Wong - 01 May 2006 21:23 GMT
> Hi, I get an "java.lang.ClassCastException: [[D" with the following
> code:
[quoted text clipped - 12 lines]
> vocabArray2 = (float[][])(oin5.readObject()); //It gives error here
> oin5.close();

   You could call .getClass().getName() to find out the name of the class
of the thing that's being returned.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.