• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected class type. Encountering ClassCastException.

 
Aniruddha Chaudhari
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aniruddha Chaudhari
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic