lundi 13 juillet 2020

Stack overflow error while invoking toString method

this is my code but when I execute the program I get many errors and I don't know why. May anyone help me?

RegisteredUser.java

public class RegisteredUser {
    private String nickname;
    ArrayList<ReviewDAO> reviews;

    public RegisteredUser(String nickname) {
        this.nickname = nickname;
        reviews = new ArrayList<>();
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void addReview(ReviewDAO review) {
        if (!this.reviews.contains(review)) {
            this.reviews.add(review);
            review.addRegisteredUser(this);
        }
    }

    @Override
    public String toString() {
        return "RegisteredUser{" +
                "nickname='" + nickname + '\'' +
                ", reviews=" + reviews +
                '}';
    }
}

ReviewDAO.java

public abstract class ReviewDAO {
    RegisteredUser registeredUser;

    public abstract boolean write(Review review);

    public void addRegisteredUser(RegisteredUser registeredUser) {
        this.registeredUser = registeredUser;
    }

    @Override
    public String toString() {
        return "ReviewDAO{" +
                "registeredUser=" + registeredUser +
                '}';
    }
}

Review.java

public class Review {
    private String title;
    private String description;
    private int rank;
    private boolean isAnonymous;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public boolean isAnonymous() {
        return isAnonymous;
    }

    public void setAnonymous(boolean anonymous) {
        isAnonymous = anonymous;
    }
}

ReviewDAO_MongoDB.java

public class ReviewDAO_MongoDB extends ReviewDAO {
    @Override
    public boolean write(Review review) {
        return false;
        // todo
    }
}

ReviewDAO_Factory.java

public class ReviewDAO_Factory {

    public ReviewDAO getReviewDAO(String technology) throws ExecutionControl.NotImplementedException {
        if (technology.equals("mongodb"))
            return new ReviewDAO_MongoDB();
        else
            throw new ExecutionControl.NotImplementedException("");
    }
}

BusinessLogic.java

public class BusinessLogic {
    public static void main(String[] args) throws ExecutionControl.NotImplementedException {
        ReviewDAO_Factory reviewDAO_factory = new ReviewDAO_Factory();
        RegisteredUser registeredUser = new RegisteredUser("Alessandro");

        registeredUser.addReview(reviewDAO_factory.getReviewDAO("mongodb"));
        System.out.println(registeredUser.toString());
    }
}

I am getting

Exception in thread "main" java.lang.StackOverflowError at RegisteredUser.toString(RegisteredUser.java:33) at java.base/java.lang.String.valueOf(String.java:2951) at ReviewDAO.toString(ReviewDAO.java:15) ...

Process finished with exit code 1

errors

Aucun commentaire:

Enregistrer un commentaire