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

After you uncomment the code in Main.java and Forum.java, fix the code as described in the comments of Main.java.

Iam getting this error "are you sure you used the arguments passed in from the main method in Main.java? args[0] is the firstName and args[1] should be the last name"

Forum.java
public class Forum {
  private String mTopic;

  public String getTopic() {
      return mTopic;
  }

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

  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());

  }
}
User.java
public class User {
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
    firstName = this.firstName;
    lastName = this.lastName;    
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}
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;
  }


}
Main.java
public class Main {

  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("Shahziab", "Awan");
    // Add the author, title and description
    ForumPost post = new ForumPost(author, "This is your forum post Title", "This is your forum post Description");
    forum.addPost(post);
  }

}

9 Answers

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Sure. So there are my pages working:

Forum.java

public class Forum {
  private String topic;

  public Forum(String topic){
    this.topic = topic; // <--- same here :)
  }

  public String getTopic() {
    return topic;
  }


  /* Uncomment this when you are prompted to do so
  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()
    );
  }

}

User.java:

public class User {
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    this.firstName = firstName; // <--- your code will be different here
    this.lastName = lastName; //<---- and here
  }

  public String getFirstName(){
    return firstName;
  }

  public String getLastName(){
    return lastName;
  }

}

ForumPost.java:

public class ForumPost {
  private User author;
  private String title;
  private String description;

  public ForumPost(User author, String title, String description){
    this.author = author; //<--- your code will be different here
    this.title = title; // <---- and here
    this.description = description; //<---- aaand here 

  }

  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
    String firstName = args[0];
    String lastName = args[1];
    User author = new User(firstName, lastName); //<----- all cruicial points marked with comments
    // TODO: initialize the forum post with the user created above and a title and description of your choice

    ForumPost post = new ForumPost(author, "Programming is awesome", "Cuz it makes us create new stuff");
    forum.addPost(post);


  }

}

Last error it was throwing was cuz u didnt declare lastName and firstName to args[], as it asked in the task. Then it would start throwing compile errors, cuz u never declared mVariable in your classes (bare in mind that mVariable is just an example of variable name) in order to dodge errors Im using this.variable in methods so compiler knows that even if the names of variables are the same withing the class, they linked to different methods and are different.

I would suggest you save these files and do the task again on your own without looking at it, try saying out loud or thinking of what do u want to achieve. It is hard at the beginning but the more u practice, the easier it gets, trust me Im a newbie just like you :)

Have fun and best of luck!

-Alex

Thanks a lot alex it works, you made my day :)

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

In user.java you assign firstName and lastName to this.firstName and this.lastName. It should be the other way around.

And tbh i would keep the same format of assigning private fields everywhere. So instead of mAuthor etc assign them using this.author etc. Dont forget lower casing ;)

Hope it solves the issue

Alex, thanks a lot for your time but this time i get this error "java.lang.NullPointerException (Look around Forum.java line 15)". Can u please send me complete solved solution please ?

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Heya, Shahzaib

U did it all geewd, there is one small bit. In Main.java, where u declare new author, instead of puting strings, put **firstName and lastName and it will work like magic :)

    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, "This is your forum post Title", 
                                           "This is your forum post Description");
    forum.addPost(post);

Hope this helped, -Alex

I removed the string and added simply firstName and lastName, but throw errors "./Main.java:10: error: cannot find symbol User author = new User(firstName, lastName);"

Ansh ⠀
Ansh ⠀
6,521 Points

Are you sure shahzaib? I guess you forgot to >'initialize' those variables.

Check if you did this before passing them as arguments in your main as stated above by Alex- ''' String firstName = args[0]; String secondName = args[1]; '''

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

I am so baffled with this task. This is what I have in the 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);
        }

        //Uncomment this when prompted
        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, "Java", "Something");
        forum.addPost(post);


    }

}

I don't see any parameters in the args only print commands beneath . Can anyone explain this to me? How does args receive the input to be stored? I get the idea of calling a method then inserting the parameters it needs passed to it that's simple, but I don't understand the args or this new User author parameter. We haven't covered this User class, I only know String.

Any help is appreciated.

anyone has a solved solution for Main.java? i'm still getting errors from solutions above. Please help!

Simply do this. User author = new User(args[0], args[1]);

charles holder
charles holder
1,944 Points

I finished but am confused on the args 0 and 1.. please explain?

I need help - I get this error

./Main.java:16: error: cannot find symbol
    forum.addPost(post);
         ^
  symbol:   method addPost(ForumPost)
  location: variable forum of type Forum
Note: JavaTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

I know exactly what this error is telling me. I do not have that function set yet. Sooooo if I go to the Forum class and uncomment the function I get errors telling me to not uncomment until ready. I'm stuck and I do not have a lot of time each day to sit for hours solving

Richard Merritt
Richard Merritt
962 Points

./Main.java:16: error: cannot find symbol forum.addPost(post); ^ symbol: method addPost(ForumPost) location: variable forum of type Forum Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error