Exception in thread "main" java.lang.ClassCastException

Discussion in 'Java' started by gbattine, Jun 18, 2006.

  1. gbattine

    gbattine Guest

    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.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...
    gbattine, Jun 18, 2006
    #1
    1. Advertising

  2. On 18 Jun 2006 14:27:48 -0700 gbattine wrote:

    > temp.addElement(intest.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.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ä
    Jan =?iso-8859-1?Q?Thom=E4?=, Jun 19, 2006
    #2
    1. Advertising

  3. gbattine

    gbattine Guest

    Sintax error, 'for each' statements are only available if source level
    is 5.0!

    I have this error when i use your code.....
    gbattine, Jun 19, 2006
    #3
  4. gbattine

    gbattine Guest

    Sintax error, 'for each' statements are only available if source level
    is 5.0!

    I have this error when i use your code.....
    gbattine, Jun 19, 2006
    #4
  5. gbattine

    gbattine Guest

    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, Jun 19, 2006
    #5
  6. gbattine

    gbattine Guest

    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, Jun 19, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Howells
    Replies:
    4
    Views:
    15,897
    HiteshMehra
    Aug 21, 2007
  2. Bob Dubery
    Replies:
    5
    Views:
    572
    Bob Dubery
    Jun 19, 2004
  3. gbattine
    Replies:
    8
    Views:
    5,760
    gbattine
    Jun 22, 2006
  4. ghashami_mina
    Replies:
    0
    Views:
    494
    ghashami_mina
    Dec 24, 2007
  5. Replies:
    2
    Views:
    424
Loading...

Share This Page