Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
It looks like the return value of client.execute isn't actually an Object[], but rather an Integer. – Louis Wasserman Apr 19 '12 at 23:42
1  
Or maybe an Object – Jordão Apr 19 '12 at 23:48

1 Answer

up vote 3 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.