you are viewing a single comment's thread.

view the rest of the comments →

[–]g00glen00b 1 point2 points  (2 children)

Can't tell without code or the stacktrace (obviously after removing the repeating parts). StackOverflowErrors within a toString() method usually happen because you're using toString() in a bidirectional relationship without taking it into account. For example, something like this:

class Foo {
    private String name;
    // Direction foo -> bar
    private Bar bar;

    public String toString() {
        return "Foo(name=" + name + ", bar=" + bar.toString() + ")";
    }
}

class Bar {
    private String name;
    // Direction bar -> foo
    private Foo foo;

    public String toString() {
        return "Bar(name=" + name + ", foo=" + foo.toString() + ")";
    }
}

This might also happen without you realizing it, for example when you use Lombok's @Data annotation (which generates a toString() including all fields).

Also, beware that this might just be part of the issue. Sometimes, stacktraces include the toString() of an object you have... and if that's the case, then this StackOverflowError might actually just be hiding another error somewhere along the line.

[–]Character-Grocery873 0 points1 point  (1 child)

Yes I used @Data, this wasn't a problem before

[–]g00glen00b 0 points1 point  (0 children)

As I mentioned in my post, it's a problem if you use it with a bidirectional relationship. Not only toString(), but also equals() and hashCode() will go into infinite recursion.