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 trial

Java Java Objects Delivering the MVP Forum

Sara Worth
Sara Worth
5,225 Points

I'm not sure what to do in the last part of this challenge.

I don't know what this challenge is asking me to do. I know I'm supposed to "fix" the code in Forum.java but the directions are very vague.

Forum.java
public class Forum {
  private String topic;

  // TODO: add a constructor that accepts a topic and sets the private field topic

  public Forum(String topic) {
    this.topic = topic;
  }

  public String getTopic() {
    return topic;
  }



  public void addPost(ForumPost post) {
    System.out.printf("A new post in %s topic from %s %s about %s is available",
            topic,
            ForumPost.getAuthor().getFirstName(),
            ForumPost.getAuthor().getLastName(),
            ForumPost.getTitle()
    );
  }


}
User.java
public class User {
  // TODO: add private fields for firstName and lastName
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    // TODO: set and add the private fields
    this.firstName = firstName;
    this.lastName = lastName;

  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  // TODO: add getters for firstName and lastName

}
ForumPost.java
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;
  }
}
Main.java
public class Main {

  public static void main(String[] args) {
    System.out.println("Beginning forum example");
    if (args.length < 2) {
      System.out.println("Usage: java Main <first name> <last name>");
      System.err.println("<first name> and <last name> are required");
      System.exit(1);
    }

    Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User();
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost();
    forum.addPost(post);


  }

}

2 Answers

Let's first read the error.

Bummer! There is a compiler error. Please click on preview to view your syntax errors!

Something did not 'compile' which means there was a typo/grammatical error. The preview pane will tell us what to look for.

/Main.java:12: error: constructor User in class User cannot be applied to given types;

Let's go to our Main.java file. The user looks fine, but maybe we need to add something to it, like arguments. Let's check out the constructor in User.java to confirm.

  public User(String firstName, String lastName) { //<--------needs 2 string arguments
    // TODO: set and add the private fields
    this.firstName = firstName;
    this.lastName = lastName;
  }

The constructor needs 2 string arguments. You passed in nothing. But, where do we find the first and last name? Well, according to the commented code, they are in the args. 'args' can be found in the main method.

public static void main(String[] args) {

They are string arrays. Let's turn these string arrays into strings for the user arguments.

    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(args[0], args[1]);

We will need to do this for ForumPost also

public ForumPost(User author, String title, String description) {
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author, "title", "description");

We can now recheck our code again for errors.

Bummer! There is a compiler error. Please click on preview to view your syntax errors!

./Forum.java:19: error: non-static method getAuthor() cannot be referenced from a static context

Looks like we need to declare our getters as static in the Forum.java class. After looking at the code, we realize the problem is you are using 'ForumPost.get...' and not the parameter 'post.get...'

  public void addPost(ForumPost post) {
    System.out.printf("A new post in %s topic from %s %s about %s is available",
            topic,
            post.getAuthor().getFirstName(),
            post.getAuthor().getLastName(),
            post.getTitle()
    );
  }

Below is your final code in Main.java

    Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(args[0], args[1]);
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author, "title", "description");
    forum.addPost(post);
Sara Worth
Sara Worth
5,225 Points

Awesome, thanks that cleared it up :)

You're welcome. This is a very difficult challenge.

Clear explanations, you made me solve it after a struggle

ababab ababab
ababab ababab
986 Points

thanks man you are the only one that you explain step by step