Error in Method Get on a Collection

Asked

Viewed 48 times

2

I need to use XML in a university job and had problems with a method Get that I had to implement in a Collection.

After a while I ended up taking a look at some sites to do this and found the following tutorial, in it has a class Contact as follows below with the field phones, and is given an instruction to create a method Get for her, I created it and the structure was briefly like this:

public class Contato {
     private Collection telefones = new ArrayList();
     public Collection getTelefones() {
         return telefones;
     }
}

But in the code section below I got an error:

---Change phone type to Object---

for (Telefone fone : contato.getTelefones()) {
    tagFone = doc.createElement("Telefone");

    idFone = doc.createElement("id");
    dddFone = doc.createElement("ddd");
    numeroFone = doc.createElement("numero");
    //Insere os valores de telefones nas tags referentes
    idFone.setTextContent(String.valueOf(fone.getId()));
    dddFone.setTextContent(String.valueOf(fone.getDdd()));
    numeroFone.setTextContent(String.valueOf(fone.getNumero()));
    //Insere as tags Telefone na tag Telefone
    tagFone.appendChild(idFone);
    tagFone.appendChild(dddFone);
    tagFone.appendChild(numeroFone);
    //Insere a tag Telefone na tag pai Telefones
    tagFones.appendChild(tagFone);
}

I tried something different, but I couldn’t fix it. I believe it is in the Get method that I have implemented myself, and if the community can help me I would be grateful.

1 answer

1


Create the Collection by stating the type that will be returned to the phone object without requesting the conversion.

public Collection getTelefones() { return telefones; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.