Search code examples
javatypesincompatibletypeerror

How can i solve the incompatible types error?


I try to add a list of things inside an array **

public Wrap(String name, Wrap wrap, List<Things> things) {
        super(name);
        this.bread = bread;
        **things.addAll( Arrays.asList( things ) );**
    }

**

and I get this error: incompatible types. Required Collection<? extends topping> but 'asList' was inferred to List<T> :no instance(s) of type variable(s) exist so that List<topping> conforms to Topping


Solution

  • You are trying to call Arrays.asList() on things, which is already a List. You can simply call addAll() directly with things:

    public Wrap(String name, Wrap wrap, List<Things> things) {
        super(name);
        this.bread = bread;
        things.addAll(new ArrayList(things));
    }
    

    However this doesn't make much sense to add things to things. Perhaps you have a class variable things and meant to use the this keyword?

    this.things.addAll(things);