Java generics incompatible types (no instance(s) of type variable(s) T exist)

We Are Going To Discuss About Java generics incompatible types (no instance(s) of type variable(s) T exist). So lets Start this Java Article.

Java generics incompatible types (no instance(s) of type variable(s) T exist)

  1. Java generics incompatible types (no instance(s) of type variable(s) T exist)

    ArrayList is an implementation of List interface.
    So all ArrayList instances are List instances but all List instances are not necessarily ArrayList.
    So when you call this method :
    public static <T> List<T> inRange(List<T> list, int index, int range) {

  2. Java generics incompatible types (no instance(s) of type variable(s) T exist)

    ArrayList is an implementation of List interface.
    So all ArrayList instances are List instances but all List instances are not necessarily ArrayList.
    So when you call this method :
    public static <T> List<T> inRange(List<T> list, int index, int range) {

Solution 1

ArrayList is an implementation of List interface.
So all ArrayList instances are List instances but all List instances are not necessarily ArrayList.

So when you call this method :

public static <T> List<T> inRange(List<T> list, int index, int range) {

you cannot assign its result to an ArrayList as you are doing :

ArrayList<View> inRange = Helper.inRange(...);

Go on to program by interface and use List in both sides :

List<View> inRange = Helper.inRange(...);

Original Author davidxxx Of This Content

Solution 2

Minimize your example like (using Integer of the templated List type):

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        List<Integer> list = new ArrayList<Integer>();
        ArrayList<Integer> inRange = Helper.inRange(list, 0,1);
    }
}

class Helper {
    public static <T> List<T> inRange(List<T> list, int index, int range) {
        List<T> res = new ArrayList<T>();
        return res;
    }
}

Then even if you put template types out of the picture:

ArrayList inRange = Helper.inRange(list, 0,1);

public static List inRange(List list, int index, int range) { ... }

you see that while the helper static method returns a List, you are trying to assign it to an ArrayList, and that’s your problem, as ArrayList is a concrete implementation of List, but you cannot assign a reference to a generic List to a concrete implementation of ArrayList

Just change to:

List<View> inRange = Helper.inRange(gameView.activePlayersViews(), pos, 
        playerView.range());

and you are good to go: https://ideone.com/MXZxqz

Original Author guido Of This Content

Solution 3

It happens, because inRange() returns type List<T>. You can store result in reference with type List or any supertype.

Try to use this code:

List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos, 
            playerView.range());

Original Author Vladimir Parfenov Of This Content

Solution 4

Try to do:

List<View> inRange = Helper.inRange(gameView.activePlayersView(), pos, 
        playerView.range());

And check this:

res.add(lista.get(i));

there is no lista in this class

Original Author Razikus Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment