0

I am a student currently learning basic object oriented java, i am trying to create an OOP program which has 3 classes YouthGroup,Section and member a Youthgroup has 3 sections ,a functionality for this program should be that a member cannot be listed in the same section twice now I'm trying to create this function :

public boolean setMembers(members members) {
 boolean crouded = true;
 for(int i = 0; i<this.getMembers().size();i++) {
 if(!this.members.contains(member)) {
 this.members.add(member);
 }
 else {
 System.out.println("Member already exists in this section");
 crouded = false;
 }
 }
 return crouded;
}

Now in the main method I'm am doing the following:

public static void main(String[] args) {
 List<member> m = new ArrayList<>();
 Section s = new Section(m,"programming",3);
 member m1 = new member("tom",21,"male",s);
 member m2 = new member("kate",20,"Female",s);
 m.add(m1);
 m.add(m2);
 s.setMembers(m1);
 s.filterAge(21);
 System.out.println(s);
}

It is giving me the following error:

 Exception in thread "main" java.lang.StackOverflowError
 at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:648)
 at java.lang.StringBuilder.append(StringBuilder.java:208)
 at youthgroup.member.toString(member.java:62)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.member.toString(member.java:64)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at java.util.AbstractCollection.toString(AbstractCollection.java:462)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at youthgroup.Section.toString(Section.java:68)
 at java.lang.String.valueOf(String.java:2981)
 at java.lang.StringBuilder.append(StringBuilder.java:131)

and it goes on forever.

this is the member class :

package youthgroup;
import java.util.Objects;
/**
 *
 * @author student
 */
public class member {
 private String name;
 private int age ; 
 private String gender;
 private Section section;
 public member(String name, int age, String gender, Section section) {
 this.name = name;
 this.age = age;
 this.gender = gender;
 this.section = section;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 public String getGender() {
 return gender;
 }
 public void setGender(String gender) {
 this.gender = gender;
 }
 public Section getSection() {
 return section;
 }
 public void setSection(Section section) {
 this.section = section;
 }
 @Override
 public String toString() {
 String word = "Member name " +name;
 word+= "age : "+getAge();
 word+="Gender : "+ getGender();
 word+= " is in section : "+getSection();
 return word;
 }
 @Override
 public int hashCode() {
 int hash = 5;
 hash = 67 * hash + this.age;
 hash = 67 * hash + Objects.hashCode(this.gender);
 return hash;
 }
 @Override
 public boolean equals(Object obj) {
 if (obj == null) {
 return false;
 }
 if (getClass() != obj.getClass()) {
 return false;
 }
 final member other = (member) obj;
 if (this.age != other.age) {
 return false;
 }
 if (!Objects.equals(this.gender, other.gender)) {
 return false;
 }
 return true;
 }
}

This is the section class :

public class Section {
 private List<member>members;
 private String sectionName;
 private int maxNumber;
 public Section(List<member> members, String sectionName, int maxNumber) {
 this.members = members;
 this.sectionName = sectionName;
 this.maxNumber = maxNumber;
 }
 public List<member> getMembers() {
 return members;
 }
 public boolean setMembers(member member) {
 boolean crouded = true;
 for(int i = 0; i<this.getMembers().size();i++){
 if(!this.members.contains(member)){
 this.members.add(member);
 }else{
 System.out.println("Member already exists in this section");
 crouded = false;
 }
 }
 return crouded;
 }
public String getSectionName() {
 return sectionName;
}
public void setSectionName(String sectionName) {
 this.sectionName = sectionName;
}
public int getMaxNumber() {
 return maxNumber;
}
public void setMaxNumber(int maxNumber) {
 this.maxNumber = maxNumber;
}
@Override
public String toString() {
 String word = " section name is : "+ getSectionName();
 word+=" Max number of members it can contain : "+ getMaxNumber();
 word+= " List of members : "+getMembers();
 return word;
}
public boolean filterByAge(int a){
 boolean found = true;
 for(int i = 0; i<this.members.size(); i++){
 if(this.members.contains(a)){
 System.out.println("These members where found with this age : "+ this.members.get(i).getName());
 }else{
 System.out.println("No member found !!!");
 found = false;
 }
 }
 return found;
}

I would really appreciate any the help, thank you in advance.

asked Jan 27, 2015 at 17:20
10
  • 6
    Show the toString method of the member class. Commented Jan 27, 2015 at 17:21
  • 1
    Pay careful attention to stack traces when there are errors; this one tells you exactly where to look for the source of the problem (line 62 of the member.java file, which falls inside its toString() method). You can solve this yourself with a little bit of focus. Commented Jan 27, 2015 at 17:25
  • 4
    You haven't posted the toString() method of Section, but my guess is that member's toString() calls section's toString(), which calls member's toString(), which calls section's toString(), etc. etc. ad infinitum. This should be obvious just by reading the stack trace. Commented Jan 27, 2015 at 17:31
  • 1
    @Student That doesn't seem likely. Please recompile, run the code again, and copy the full stack trace (the error you posted above) into the question. Also, please show your toString() method in Section class, as @JB Nizet mentioned. Commented Jan 27, 2015 at 17:34
  • 1
    A member could be part of a Section, but a Section is not a part of member. So you should not have a field section in member. Furthermore, the equals method should definitely also verify that the name is identical (or use some other identifying mark such as a personal number, duplicate names are possible). Please use clear names and class names should be CamelCase starting with uppercase. Commented Jan 27, 2015 at 17:37

1 Answer 1

2

Your toString() method is making a recursive call to itself.

The toString() method calls getSection() which will return a Section object that will have toString() called on it explicitly. This toString() will itself get a List of member objects on which it will call toString(), however, this second toString() method itself will call back to the member toString() method, which will call back again, and again, and again. You see the problem, I imagine.

This will cause a StackOverflowError as it will loop infinitely deeper into the call stack.

word+= " is in section : "+getSection();
//section is returned and called toString()
word+= " List of members : "+getMembers();
//List of members are returned and called `toString()`, which will loop back to first line I've written

The important thing to understand here is what happens when you use the + operator in Java. When using between Objects, including String objects or any Objects you define, the + operator will explicitly convert its left and right hand-side arguments to Strings. The way that this is done in Java is with an implicit call to the toString() method which you have overridden, but would still be present had you not overridden it (it is in the Object superclass, so all Java objects inherit some base implementation)

answered Jan 27, 2015 at 17:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.