Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Exception in thread "main" java.lang.ClassCastException: [B

Reply
Thread Tools

Exception in thread "main" java.lang.ClassCastException: [B

 
 
gbattine
Guest
Posts: n/a
 
      06-21-2006
Hi guys,
i need a disperate help.....i'm crazying on this problem and i don't
find an exit. I know this exception mean a bad casting...but can you
help me to know how solve it?
Without your help i'm not able to go on...
please help me....

Exception in thread "main" java.lang.ClassCastException: [B
at giu.princ.getBytes(princ.java:142)
at giu.princ.main(princ.java:15

And my function is


package giu;
import java.io.BufferedReader;
import java.io.*;
import java.io.IOException;
import java.util.*;

public class princ {

private static String fileName = "Dato2.txt";
private static String file = "ris.txt";
private String geneid=null;
private static int row=0;
private static int numberOfNumericColumns=0;
private static int col=0;
Double[] values=new Double[numberOfNumericColumns];
String[]intest=null;
private ArrayList rows = new ArrayList();
Head h;
Riga r;

public boolean insRighe(Riga nuovo){
return rows.add(nuovo);
}

public List stampaRows(){
return rows;}

public Head stampaHead(){
return h;}

public void carica()throws IOException{


FileReader reader=new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);


String line = null;

while ((line = br.readLine()) != null) {

line = line.replace (',', '.');

StringTokenizer st = new StringTokenizer(line);
numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);
//se siamo nella prima riga(contatore segna 0)
if(row==0){
intest=new String[col];
int j=0;
while(st.hasMoreTokens()){
intest[j]=(st.nextToken().trim());
j++;
}
/*for(int w=0;w<=intest.length-1;w++){
System.out.println(w+" "+intest[w]);
}*/



//STAMPA LA RIGA HEAD ALL'INIZIO DELL'OUTPUT
h=new Head(intest);//crei l'oggetto head
String []qa=h.getHvalues();
String asd="";
for(int i=0;i<=qa.length-1;i++){
asd=asd.concat(qa[i]+" ");
}
System.out.println("head "+asd);//stampo contenuto dell' head
row=1;
}//fine if

else
{
Double[] values=new Double[numberOfNumericColumns];
int z=0;
geneid=st.nextToken();
while (st.hasMoreTokens()) {
String app=st.nextToken();
values[z]=Double.valueOf(app);
z++;
}
r=new Riga(geneid,values); //crei l'oggetto riga

System.out.println("riga");
System.out.println(r.getgeneid());

values=r.getvalues();
for(int e=0;e<=values.length-1;e++){
System.out.println(values[e]);
}
insRighe(r); //aggiungi
}
row++;
}
}



public byte[] getBytes(){
byte middlerow=' ';
byte endrow=';';
byte[] data;
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();


byte[] tempByte1;
for(int e=0;e<=val.length-1;e++){
//Returns a string representation of the double argument.
tempByte1 = Double.toString(val[e]).getBytes();
System.out.println("la soluzione e"+tempByte1);
for (int j = 0; j >< tempByte1.length; j++) {
temp.addElement(Byte.valueOf(tempByte1[j]));

temp.addElement(Byte.valueOf(middlerow));

}
temp.addElement(Byte.valueOf(endrow));
System.out.println("la soluzione 4e"+tempByte1);
}
}
data=new byte[temp.size()];
System.out.println("la soluzione 2e"+data);
for (int t=0;t<temp.size()-1;t++)
data[t]=(((Byte)temp.elementAt(t)).byteValue());
System.out.println("la soluzione nuova");
System.out.println(temp);
/*for (int k=0;k<=temp.size()-1;k++){
System.out.println(k+ " temp "+temp.get(k));
}*/

return data;

}
public static void main(String[] args) throws IOException {
byte x[];
String totale=null;
princ p=new princ();
p.carica();
System.out.println("Dati caricati");
x=p.getBytes();
System.out.println("Byte convertiti");
BufferedWriter out = new BufferedWriter(new FileWriter(file));; //
define the output stream
for(int i=0;i<=x.length;i++){
System.out.println(x[i]);
//out.writeByte
//out.write(" ");
} //write the array of bytes to file
}
}

 
Reply With Quote
 
 
 
 
Moiristo
Guest
Posts: n/a
 
      06-21-2006
gbattine wrote:
> Hi guys,
> i need a disperate help.....i'm crazying on this problem and i don't
> find an exit. I know this exception mean a bad casting...but can you
> help me to know how solve it?
> Without your help i'm not able to go on...
> please help me....
>
> Exception in thread "main" java.lang.ClassCastException: [B
> at giu.princ.getBytes(princ.java:142)
> at giu.princ.main(princ.java:15
>
> And my function is

<cut code>

'[B' is the string representation of the data you are casting. It means
that the data is in fact a byte array, not a byte.

> data[t]=(((Byte)temp.elementAt(t)).byteValue());


 
Reply With Quote
 
 
 
 
gbattine
Guest
Posts: n/a
 
      06-21-2006
Thanks Moiristo for your reply....but i don't understand well what's
the problem.
Can you help me explaing it and giving me a solution,if you know it?
Thanks very much..

 
Reply With Quote
 
Moiristo
Guest
Posts: n/a
 
      06-21-2006
gbattine wrote:
> Thanks Moiristo for your reply....but i don't understand well what's
> the problem.
> Can you help me explaing it and giving me a solution,if you know it?
> Thanks very much..
>


In rules 109, 119 and 129, you include a bytearray (the result of
getBytes()) to your temp Vector. An index of a Vector is not a start
position, it refers to an object, which can be a whole array. Therefore,
instead of adding the bytearray from String.getBytes(), you need to add
every byte in the resulting bytearray separately to the Vector. This can
be done by using the function Vector.addAll(<collection>). And AFAIK,
you don't have to use the addElement method for adding a byte, you can
just use the method Vector.add(<object>). Furthermore, I see no real
benefit in giving the Vector an inital capacity; a Vector grows
automatically.
 
Reply With Quote
 
gbattine
Guest
Posts: n/a
 
      06-21-2006
Thanks!!!
I'm understanding.......
but i'm not expert in java,can you help me?
If i've understood i've to modify

temp.addElement(intest[i].getBytes());

into

byte[] bytesnew=intest[i].getBytes();
temp.addAll(bytes2);

but the addAll function return a boolean?
How can i do in this specific example?
Can you help me with code?
Thanks........

 
Reply With Quote
 
gbattine
Guest
Posts: n/a
 
      06-21-2006
There is an error....i want say

temp.addElement(intest[i].getBytes());

into

byte[] bytesnew=intest[i].getBytes();
temp.addAll(bytesnew);

And for

temp.addElement(g.getBytes());
and
tempByte1 = Double.toString(val[e]).getBytes();

can you help me?
I'm stopped and i need to go on..............thanks too much

 
Reply With Quote
 
gbattine
Guest
Posts: n/a
 
      06-21-2006

I try and try but nothing.........can someone help me?

 
Reply With Quote
 
Moiristo
Guest
Posts: n/a
 
      06-21-2006
gbattine wrote:
> There is an error....i want say
>
> temp.addElement(intest[i].getBytes());
>
> into
>
> byte[] bytesnew=intest[i].getBytes();
> temp.addAll(bytesnew);
>
> And for
>
> temp.addElement(g.getBytes());
> and
> tempByte1 = Double.toString(val[e]).getBytes();
>
> can you help me?
> I'm stopped and i need to go on..............thanks too much
>


I'm sorry, but addAll needs a Collection, you cannot add an array. It's
better to create a for loop like this:

foreach(byte b : bytesnew) temp.add(new Byte(b));
 
Reply With Quote
 
gbattine
Guest
Posts: n/a
 
      06-22-2006
Thanks Moiristo...
i've solved my exception but i'm not sure the conversion file to array
of byte works fine.
I need to develop inverse function, i've the array of byte and i want
to rebuild the original format.
How can i do?
I know each string can be coded into more byte like the double...i only
know that whitespace(in byte 32) separates different values and ;(in
byte:59) separate different rows.
I think for the first load the array of double into a string,what's the
code?


public String(byte[] bytes)?

Can you help me with initial code to separate different rows basing on
;(59)?
Thanks very much for your availability.......

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments