aspose file tools*
The moose likes Java in General and the fly likes Unexpected class type. Encountering ClassCastException. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unexpected class type. Encountering ClassCastException." Watch "Unexpected class type. Encountering ClassCastException." New topic
Author

Unexpected class type. Encountering ClassCastException.

Aniruddha Chaudhari
Greenhorn

Joined: May 19, 2010
Posts: 12
Hi,

I am writing a java application in which I call Matlab function via JMI (Java Matlab Interface).
In Matlab, every variable is treated as an array. So, after the (custom made) function (involving complex image processing operations) in matlab is completed, it returns an array, say a, of length 2, in which a[0] is of type [Ljava.lang.String and a[1] is of type [Ljava.lang.Object.
a[1] again contains objects of [Ljava.lang.Object where I expect array of floats.
When i try to cast them to Float array, i get ClassCastException.

How do I retrieve float values out of the object array which is non compliant to casting to Float array?

Any suggestions would be of great help.
THanks,


Certifications: SCJP 6, SCWCD 5
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Maybe you're looking for the float[] (notice the primitive not the wrapper). If that also fails you can check the classname with object.getClass().getName() and classed it to that class.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Aniruddha Chaudhari
Greenhorn

Joined: May 19, 2010
Posts: 12
Wouter Oet wrote:Maybe you're looking for the float[] (notice the primitive not the wrapper). If that also fails you can check the classname with object.getClass().getName() and classed it to that class.


Thanks for your reply.
No, I am using Float[] (wrapper class) and i already checked the class name. that is [Ljava.lang.Object. Isn't there any other way?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I looked it up and this is what I found:

Z = boolean
B = bytebyte
C = char
D = double
F = float
I = int
J = long
S = short
class/interface = Lclassname;

[F = float[]
[[F = float[][]

so [Ljava.lang.Object is an array of objects.
Aniruddha Chaudhari
Greenhorn

Joined: May 19, 2010
Posts: 12
Wouter Oet wrote:I looked it up and this is what I found:

Z = boolean
B = bytebyte
C = char
D = double
F = float
I = int
J = long
S = short
class/interface = Lclassname;

[F = float[]
[[F = float[][]

so [Ljava.lang.Object is an array of objects.


Thanks for the reply.
yes, I understand. But the values in matlab are floats which are being returned to the Java application.
1) Since those values are treated as arrays in Matlab, they are received in java as array of objects even though the value returned is just one float.
2) When I type cast that array of objects to Float array, it gives me ClassCastException.
3) When I invoke getClass.getName(), gives me [Ljava.lang.Object and not [Ljava.lang.Float. So I am stuck, even though the value that is being returned from Matlab is Float, I am unable to retrieve it in Java application.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Here's a guess (I know nothing about MatLab). Rather than trying to cast the array of objects to array of floats/Floats, try casting the first element of the array from an Object to float/Float
Aniruddha Chaudhari
Greenhorn

Joined: May 19, 2010
Posts: 12
Tom Reilly wrote:Here's a guess (I know nothing about MatLab). Rather than trying to cast the array of objects to array of floats/Floats, try casting the first element of the array from an Object to float/Float


Thanks for the suggestion. I found there are again arrays of objects (instead of one object) in the previous array of object. It seems even one single value returned from matlab is perceived as array of Objects (not the actual type) in java.
But still the first value in the first array returned is array of Strings, so I was thinking, may be somehow I shall be able to extract Floats out of the second value. But alas, seems nobody has used this or some bug still unexplored.
 
 
subject: Unexpected class type. Encountering ClassCastException.