0

I have these two interfaces:

public interface FieldValue<T> {

  public String getLabel();
  public T getHrValue();
  public T getPicValue();
  public T getMinusOneValue();
  public T getMinusTwoValue();

}

and

public interface Field<T> {

  public List<FieldValue<T>> getValues();
  public void injectHrValue(ReportData data);
  public void injectPicValue(ReportData data);
  public void injectMinusOneValue(ReportData data);
  public void injectMinusTwoValue(ReportData data);
  public void debug();

}

I have then a class which contains a list of such Field type. If I try to iterate in this way getValues() is not able to infer the FieldValue type but only Object.

        for (Field f : myclass.getFields()) {
            for(FieldValue fv : f.getValues()) { //error, cannot convert element type Object to FieldValue
            }
        }

Can someone explain me why this happens?

Thanks

Nemax
  • 61
  • 2
  • What type is the `Field`'s generic type? You have specified `Field` so I presume you need to specify the concrete type of `T` in your second code snippet? – jbx Sep 18 '18 at 09:51
  • 1
    Declare the loop variables as `Field> f` and `FieldValue> fv` (or with a specific type parameter, if one is available). – Andy Turner Sep 18 '18 at 09:53
  • See: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). You are using raw types `Field` and `FieldValue` in your last code snippet. – Jesper Sep 18 '18 at 09:53
  • @AndyTurner Not when I was typing my comment ... :-) – Jesper Sep 18 '18 at 09:54

0 Answers0