CircularReferenceException error

-1

I need some help, I have this: com.thoughtworks.xstream.core.TreeMarshaller $ CircularReferenceException And I can not find the solution. Here is the snippet of code:

    XStream xstream = new XStream(new JettisonMappedXmlDriver());
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.alias("resultList", ResultList.class);
    xstream.alias("resultRow", ResultRow.class);
    xstream.alias("resultCol", ResultCol.class);

    return xstream.toXML(lista);

Exception occurs in xstream.toXML.

The classes in Java:     public class ResultList implements Serializable {

private List<ResultRow> rows = null;

public List<ResultRow> getRows() {
    if (rows == null) {
        rows = new ArrayList<ResultRow>();
    }
    return rows;
    }

}
public class ResultRow implements Serializable {

private List<ResultCol> columns;

public List<ResultCol> getColumns() {
    if (columns == null) {
        columns = new ArrayList<ResultCol>();
    }
    return columns;
}

public void addColumn(ResultCol col) {
    col.setOrdem(getColumns().size() + 1);
    getColumns().add(col);
}

}

public class ResultCol implements Serializable {

private int ordem = 0;
private T valor;
private String nome;
private String typeName;
private String format;
private String label;
private int type;
private ResultRow row;

public ResultCol(ResultRow row, T valor, String nome, String typeName, int type) {
    this.valor = valor;
    this.nome = nome;
    this.typeName = typeName;
    this.type = type;
    this.format = "";
    this.label = "";
}

public ResultCol(ResultRow row, T valor, String nome, String typeName, int type, String format, String desc) {
    this.valor = valor;
    this.nome = nome;
    this.typeName = typeName;
    this.type = type;
    this.format = format;
    this.label = desc;
}

public T getValor() {
    return valor;
}

public void setValor(T valor) {
    this.valor = valor;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public int getOrdem() {
    return ordem;
}

public void setOrdem(int ordem) {
    int ordemAtual = this.ordem;

    if (ordem < ordemAtual) {

    } else if (ordem > ordemAtual) {

    }

    this.ordem = ordem;
}

public String getTypeName() {
    return typeName;
}

public int getType() {
    return type;
}

public String getFormat() {
    return format;
}

public void setFormat(String format) {
    this.format = format;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

public ResultRow getRow() {
    return row;
}

@Override
public String toString() {
    if (valor == null) {
        return "";
    } else {
        return valor.toString();
    }
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 83 * hash + (this.nome != null ? this.nome.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (obj instanceof String) {
        return this.nome.equals(obj);
    }
    if (getClass() != obj.getClass()) {
        return false;
    }

    final ResultCol<?> other = (ResultCol<?>) obj;
    if ((this.nome == null) ? (other.nome != null) : !this.nome.equals(other.nome)) {
        return false;
    }
    return true;
}

}

If you are missing something, just ask, I can not find the reason for the error.

    
asked by anonymous 05.06.2017 / 15:50

1 answer

3

Its ResultRow and ResultCol classes have a circular reference, for example:

private ResultRow row; //classe ResultCol
private List<ResultCol> columns; //classe ResultRow

Probably what is happening is that some instance of ResultCol has an instance of ResultRow and that instance again has reference to the same instance of ResultCol resulting in a circular reference.

An easier way to explain would be, Person has Phone, Phone has Person, Phone instance that is referenced in Person, also references that same Person instance. So, by the time XStream parse these objects, it goes into an infinite loop.

Debug and see if that's the reason.

    
05.06.2017 / 16:09