Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.Integer cannot be cast to [Ljava.lang.Object;

Tags:

java

xml-rpc

I'm currently retrieving data using XML-RPC, this is what I have:

Object[] params = new Object[]{param1, param2};
Object[] obj = new Object[]{};

try {
    obj = (Object[]) client.execute("method.name", params);
} catch (XmlRpcException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

return obj;

The problem is that sometimes it will return -1 and I'll get this error: java.lang.Integer cannot be cast to [Ljava.lang.Object; - I was wondering if there was a way around this problem?

like image 821
Michael Avatar asked Apr 19 '12 23:04

Michael


1 Answers

You have to check the type of the return value before casting.

Object result = client.execute(...);
if (result instanceof Integer) {
  Integer intResult = (Integer) result;
  ... handle int result
}    
else if (result instanceof Object[]) {
  obj = (Object[]) result;
}
else {
  ... something else
}

I'd be tempted to create a strongly-typed API around these RPC calls. But then again, maybe that's what you're already doing...

like image 65
Jordão Avatar answered Sep 22 '22 18:09

Jordão