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 / June 2006

Tip: Looking for answers? Try searching our database.

Exception in thread "main" java.lang.ClassCastException

Thread view: 
gbattine - 18 Jun 2006 22:27 GMT
Can someone tell me how solve this problem?
I've a function that return an array of byte b[] and a main function
like this

byte x[]=null;
x=p.getBytes();

where p.getBytes() return the array of byte....how can i do?
Here is the code of getBytes() and main function...

public byte[] getBytes(){
        byte middlerow=' ';
        byte endrow=';';
        Vector temp=new Vector(100000);
        int i=0;
        String g=null;
        Riga r;
        Double val[];
        while(i<intest.length){
            temp.addElement(intest[i].getBytes());
            temp.addElement(Byte.valueOf(middlerow));
            i++;
        }
        temp.addElement(Byte.valueOf(endrow));
        System.out.println("Intestazione convertita in byte");

        for(int l=0;l<rows.size()-1;l++){
            r=(Riga)rows.get(l);
            g=r.getgeneid();
           temp.addElement(g.getBytes());
           temp.addElement(Byte.valueOf(middlerow));
           val=r.getvalues();

           for(int e=0;e<=val.length-1;e++){

               temp.addElement(Byte.valueOf(val[e].byteValue()));
               temp.addElement(Byte.valueOf(middlerow));
           }
            temp.addElement(Byte.valueOf(endrow));
        }

        byte [] b=new byte[temp.size()];
        for (int k=0;i<temp.size();i++){
           b[k]=(((Byte)temp.elementAt(k)).byteValue());
        }
        return b;

                }

        public static void main(String[] args) throws IOException {
            princ p=new princ();
            p.carica();
            System.out.println("Dati caricati");
            byte x[]=null;
            x=p.getBytes();
            System.out.println("Byte convertiti");
            FileOutputStream targetFile= new FileOutputStream(file); // define
the output stream
            targetFile.write(x); //write the array of bytes to file
            targetFile.flush();
            targetFile.close(); //close the File stream

        }

Thanks very much...
Jan Thomä - 19 Jun 2006 07:16 GMT
>             temp.addElement(intest[i].getBytes());
>             temp.addElement(Byte.valueOf(middlerow));

Hi,
the problem is within the two lines shown above. First line inserts a
Byte[] into the temp vector, second line a single Byte. You need to add the
single Bytes to the vector like:

Byte []bytes = iterest[i].getBytes();
for( Byte aByte : bytes ) {
   temp.addElement( aByte );
}
temp.addElement( Byte.valueOf( middlerow ) );

This would solve your classcastexception. I don't know if this solves your
business problem as well.

Best regards,
Jan Thomä
gbattine - 19 Jun 2006 09:14 GMT
Sintax error, 'for each' statements are only available if source level
is 5.0!

I have this error when i use your code.....
gbattine - 19 Jun 2006 09:14 GMT
Sintax error, 'for each' statements are only available if source level
is 5.0!

I have this error when i use your code.....
gbattine - 19 Jun 2006 09:25 GMT
Greaaaaaaaaaaaaaaaaat!!!!!!!!
I've solved my problem thanks to you!!!!!!!!!!!
I have an other question for you.
I have to develop the inverse function,that read this array of byte and
rebuild original format.
How can i see if the array of byte obtained is what i want?
That is how can i "test" if my precedent function getBytes has worked
fine?
Thanks....
gbattine - 19 Jun 2006 09:25 GMT
Greaaaaaaaaaaaaaaaaat!!!!!!!!
I've solved my problem thanks to you!!!!!!!!!!!
I have an other question for you.
I have to develop the inverse function,that read this array of byte and
rebuild original format.
How can i see if the array of byte obtained is what i want?
That is how can i "test" if my precedent function getBytes has worked
fine?
Thanks....


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



©2008 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.