0

While working on generics and Callable i am getting following error:

The type of the expression must be an array type but it resolved to capture#1-of ? extends Object[]

 public class Test {

    public Object m1(Callable<? extends Object[]> abc) throws Exception {
        return abc.call()[0];//Getting error here
    }
}

I am really not sure what is producing this error.

It would be great if some one share knowledge on this error as i am not able to understand it.

It is not duplicate of above mentioned question , Please check

Sachin Sachdeva
  • 10,526
  • 37
  • 100
  • @RobinTopper--No No its not duplicate, Please check again I am asking from generics context and not about extending A[] – Sachin Sachdeva May 02 '17 at 10:26
  • This error is because method `call()` returns type `? extends Object[]` which can't be cast to array. If you change to `Callable` it works fine – Jay Smith May 02 '17 at 10:29
  • Do you want to know "_what is producing this error_" or _how to solve it_? – Stefan Warminski May 02 '17 at 10:49
  • Here is some background on the [interaction of generics and arrays](http://stackoverflow.com/a/18581313/2074605) and some [additional](http://stackoverflow.com/a/530289/2074605) [examples](http://stackoverflow.com/a/1817544/2074605) that may be helpful. – vallismortis May 02 '17 at 10:51

2 Answers2

0

You can't extend an array. Just use Callable<Object[]>.

UPDATE:

Or, if it's the class of the elements that can be extended, You can do this:

public <T> T m1(Callable<T[]> abc) throws Exception {
    return abc.call()[0];
}
Maurice Perry
  • 7,501
  • 2
  • 9
  • 19
0

I would suggest a little debugging from yourself

public class Test {
    public Object m1(Callable<? extends Object[]> abc) throws Exception {
        Object[] call = abc.call(); // Maybe your error is here
        System.out.println(call.length); // See what this returns
        return call[0];
    }
}

Also remember that when you say //Getting error here, tell us WHAT error you're getting.

Danon
  • 1,793
  • 14
  • 28