Error of Circularreferenceexception

Asked

Viewed 52 times

-1

I need help, I’m with this : com.thoughtworks.Xstream.core.Treemarshaller$Circularreferenceexception And I can’t find the solution. This is the excerpt from the 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 some information is missing just ask, I can’t find the reason for the mistake.

1 answer

3


Their classes ResultRow and ResultCol have 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 this instance has, again the 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, the instance of Phone that is referenced in Person, also references that same instance of Person. Therefore, the moment Xstream will parse these objects, it enters an infinite loop.

Debug and see if this is the reason.

  • The reason really is this, I am now looking for the solution of it without needing to change a lot, example: an annotation that makes the ignore.

Browser other questions tagged

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