Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBen Morse
6,068 PointsChallenge task 3 of 4 error in my code?
Task 3: Alright so now let's focus on ForumPost. It's going to need a constructor that takes a User author a String title and a String description. Don't forget to initialize the member fields in the constructor. Also please add the getter for mDescription.
Here is my code:
public class ForumPost {
private User mAuthor;
private String mTitle;
private String mDescription;
public ForumPost (User author) {
mAuthor = author;
}
public ForumPost (String title) {
mTitle = title;
}
public ForumPost (String description) {
mDescription = description;
}
public User getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getDescription() {
return mDescription;
}
// TODO: We need to expose the description
}
here is my ERROR:
./ForumPost.java:13: error: constructor ForumPost(String) is already defined in class ForumPost
public ForumPost (String description) {
^
1 error
I'm confused....that shouldn't be a problem because i'm passing a different variable. Right?
Ben Morse
6,068 Pointsthanks for the tip :)
2 Answers
qlpxjevhuv
10,503 PointsThe challenge is expecting you to create one constructor that has three parameters.
public constructorName(param1, param2, param3) {
m1 = param1;
m2 = param2;
m3 = param3;
}
Currently you have three constructors, each taking one parameter.
Ben Morse
6,068 Pointsah, i was doing to much then. Thanks!
Andrei Li
34,475 Pointsauthor is an object, so it should be type of User. The correct answer is:
public class ForumPost {
private User author;
private String title;
private String description;
// TODO: add a constructor that accepts the author, title and description
public ForumPost(User author, String title, String description) {
this.author = author;
this.title = title;
this.description = description;
}
public User getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
Robert Richey
Courses Plus Student 16,352 PointsRobert Richey
Courses Plus Student 16,352 PointsHi Ben,
I added markdown to help make the code more readable. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.
Cheers!