Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible types: Required: T Found: Object - IntelliJ

There is this method (abbreviated) in the project I'm working on:

   public <T> T query(
        final Extractor<T> extractor, final List result) {
                //...
               return extractor.extract(result) 
                //...
    }

with the Extractor defined as:

public interface Extractor<T> {
    T extract(List<Map<String, Object>> result);
}

In Eclipse there isn't any error but IntelliJ refuses to compile the class with Incompatible types: Required: T Found: Object, the only way to is if I cast the return value to T or return Object instead and I can't figure out why it is failing.

like image 271
lifesoordinary Avatar asked Aug 22 '17 11:08

lifesoordinary


2 Answers

I'm getting the same error in Eclipse.

It is probably caused by using the raw List type.

Changing the signature of the query method to:

public <T> T query(final Extractor<T> extractor, final List<Map<String, Object>> result)

fixes it.

like image 139
Eran Avatar answered Nov 07 '22 13:11

Eran


You need to add type argument to the parameter:

query(final Extractor<T> extractor, final List<Map<String, Object>> result){

The error message because of type mismatch(Raw Type). Here is what docs say about raw type:

Raw types show up in legacy code because lots of API classes (such as the Collections classes) were not generic prior to JDK 5.0. When using raw types, you essentially get pre-generics behavior — a Box(in your case List) gives you Objects. For backward compatibility, assigning a parameterized type to its raw type is allowed

like image 2
Blasanka Avatar answered Nov 07 '22 13:11

Blasanka