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 (Retired) Delivering the MVP Wrapping up

Don't know why I am constantly getting an error .. need help right away

HELP!!!

ForumPost.java
public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

  public ForumPost(User Author, String title, String description)
  {
    mAuthor = Author;
    mTitle = title;
    mDescription = description;
  }



  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
   public String getDescription()
  {
    return mDescription;
  }

}
User.java
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;
  }
}
Forum.java
public class Forum {
  private String mTopic;



  public void addPost(ForumPost post) {
      //When all is ready uncomment this...
      System.out.printf("New post from %s %s about %s.\n",
                         post.getAuthor().getFirstName(),
                         post.getAuthor().getLastName(),
                         post.getTitle());

  }
  public Forum(String topic)
  {
    mTopic = topic;
  }
   public String getTopic() {
      return mTopic;
  }

}
Example.java
public class Example {

  public static void main(String[] args) {
     Forum forum = new Forum("Java");
    // Take the first two elements passed args
    User author = new User(firstName,lastName);
    // Add the author, title and description
     ForumPost post = new ForumPost(Author, title, description);
    forum.addPost(post);
    System.out.println("Starting forum example...");
    if (args.length < 2) {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
    }

  }
}

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Chevano,

There are two things that are causing the error.

The first thing is this line:

User author = new User(firstName,lastName);

What the code checker wants you to do, is to put the args from the main class into the User constructor. That probably didn't make much sense so let's take a deeper look.

Further down your code you have this line:

System.out.println("Starting forum example...");
    if (args.length < 2) {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
    }

First thing's first, let's move that entire code block up to the top of the class. What this is doing is that when the main class is run, it expects an array of strings which we call "args" as you can see here (String[] args). During the java course, we don't really pass in anything to the main file but because we have an if statement that checks to make sure our array length is greater than 2, we have to pass in an array. When we run the program, the codechecker will automatically give an array of length of 2.

Another thing is that in the user class, this is how the constructor looks like:

public User(String firstName, String lastName)

that means it only accepts two strings.

what you wrote was this:

User author = new User(firstName,lastName)

it would work if you put quotations around those two words but what this code challenge wants is for you to put the strings in the args array inside the User constructor. In case your still confused, it would look like this:

User author = new User(args[0], args[1])

what this does is that when the main class is run, it will take the first and second strings from the args array and use that as the first and last name. I hope this makes some sense to you.

the last thing is this line:

ForumPost post = new ForumPost(Author, title, description);

Look back at the ForumPost constructor, what does it accept? it accepts an User object, followed by two strings. Above, we just created our User class. so we pass that in as the User object. then after that, write a title string and a description string.

That should solve your errors and I hope you learned a thing or two as well as understanding this. It's tricky so dont worry if you dont completely get it.

If this helped, please mark me as best answer :)

And if you have any questions regarding this, I'd be happy to help

Thanks and Happy Coding!!

Kevin