Exception in thread "main" java.lang.ClassCastException

G

gbattine

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...
 
J

Jan =?iso-8859-1?Q?Thom=E4?=

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ä
 
G

gbattine

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

I have this error when i use your code.....
 
G

gbattine

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

I have this error when i use your code.....
 
G

gbattine

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....
 
G

gbattine

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top