0

Code:

 public List<InvoicePostAddressForm> invoicePostAddressListByStoreId(String storeId) throws BusinessException {
            List voList = new ArrayList();
            if (!result.getSuccess()) {  
                throw new BusinessException(result.getCode());
            }
            voList = (List)result.getResult();
            ArrayList<InvoicePostAddressForm> formList = new ArrayList<InvoicePostAddressForm>();
            for (InvoicePostAddressVo vo : voList) {  // here's error
                formList.add(this.ipavo2Form(vo));
            }
            return formList;
        }

Code is from a decompiled project, i dont have sorce code, and

for (InvoicePostAddressVo vo : voList)

show an error in editor , i use Idea

Incompatible types.

Required:Object

Found:InvoicePostAddressVo

InvoicePostAddressVo is a object, and it requires a object, why show this error?

  • Because you haven't specified the type of the list `List voList = new ArrayList();` – Mark Jun 06 '19 at 09:56
  • As already mentioned, you should not use raw types. I.e. specify the generic type of a generic class **always**. So change `List` to `List` – Lino Jun 06 '19 at 10:01

1 Answers1

4

The reason why is this line:

List voList = new ArrayList();

Did you intend to do this, as opposed to something like this?

List<InvoicePostAddressVo> voList = new ArrayList<>();

When you try to iterate over the collection with the line

for (InvoicePostAddressVo vo : voList)

The compiler is expecting that voList guarantees all elements will be of type InvoicePostAddressVO, which is not the same as a list of Object.

You are strongly discouraged from using a raw List without good reason.

adickinson
  • 553
  • 1
  • 3
  • 14