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 trialNeelesh Tewani
1,239 Pointsi want to know the problem in the fourth task of this challenge
ForumPost post = new ForumPost(author.getFirstName(),author.getLastName(),"challenge completed","yes i have done the challenge");
public class ForumPost {
private User mAuthor;
private String mTitle;
private String mDescription;
public ForumPost(User nAuthor,String title,String description){
nAuthor=mAuthor;
mTitle = title;
description= mDescription;
}
public String getDescription(){
return mDescription;
}
public User getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
// TODO: We need to expose the description
}
public class User {
private String mFirstName;
private String mLastName;
public User(String firstName, String lastName) {
// TODO: Set the private fields here
mFirstName = firstName;
mLastName = lastName;
}
public String getFirstName(){
return mFirstName;
}
public String getLastName(){
return mLastName;
}
}
public class Forum {
private String mTopic;
public String getTopic() {
return mTopic;
}
public Forum(String ltopic){
mTopic = ltopic;
}
public void addPost(ForumPost post) {
System.out.printf("New post from %s %s about %s.\n",
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle());
}
}
public class Example {
public static void main(String[] args) {
System.out.println("Starting forum example...");
if (args.length < 2) {
System.out.println("first and last name are required. eg: java Example Craig Dennis");
}
Forum forum = new Forum("Java");
// Take the first two elements passed args
User author = new User("Neelesh","Tewani");
// Add the author, title and description
ForumPost post = new ForumPost(author.getFirstName(),author.getLastName(),"challenge completed","yes i have done the challenge");
forum.addPost(post);
}
}
4 Answers
Kevin Faust
15,353 PointsHey Neelesh,
So first off, let's look at this:
ForumPost post = new ForumPost(author.getFirstName(),author.getLastName(),"challenge completed","yes i have done the challenge");
Before we do anything, analyze what you wrote, You are basically putting 4 strings as constructor parameters. Look at our ForumPost class:
public ForumPost(User nAuthor,String title,String description)
It takes a User object, and two Strings right?
Look at this:
User author = new User("Neelesh","Tewani");
Here we created a User object right? That is what we pas in as our first paramter
Here is what it should look like:
ForumPost post = new ForumPost( author , "string text" , "string text" );
Ok it looks great now right? Oh wait it's still not working.
[ https://teamtreehouse.com/community/output-is-appropriate-in-editor-but-still-fails-challenge ]
Here's a link to my answer that will guide you through the remaining of what you need to do. I've written a in-depth explanation of what you will have to do.
Let me know if you got everythin working,
Happy coding and best of luck,
Kevin
William Laub
7,793 PointsI see a few issues here. First, your
ForumPost
constructor just takes the
User
as its first argument, so you should have
ForumPost post = new ForumPost(author,"challenge completed","yes i have done the challenge");
in Example.java.
Second, the task asks you to create the author
User
from the arguments passed to main.
Finally you've got
nAuthor
and
mAuthor
switched in the
ForumPost
constructor so you set the argument equal to the member rather than the other way around. Once you've fixed those three issues it should work.
Neelesh Tewani
1,239 PointsI still didn't understand can you show me practically
Neelesh Tewani
1,239 Points/* still not solved Bummer! java.lang.NullPointerException (Look around Forum.java line 15)*/
Forum forum = new Forum("Java"); // Take the first two elements passed args User author = new User("neelesh","tewani"); // Add the author, title and description ForumPost post = new ForumPost(author,"challenge accepted","you have completed the challenge"); forum.addPost(post); } }
Kevin Faust
15,353 PointsNeelesh; I need you to go click the link I posted above. I have a in depth expanation on how to solve your current problem. thanks