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

William Mariadason
William Mariadason
1,393 Points

Why do I get a NullPointerException here?

Beginning forum example java.lang.NullPointerException at Forum.addPost(Forum.java:17) at Main.main(Main.java:15) at JavaTester.run(JavaTester.java:185) at JavaTester.main(JavaTester.java:45)

I don't understand why I get this

Forum.java
public class Forum {
  private String topic;

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

  public String getTopic() {
    return topic;
  }

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

  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 {
  // 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
    firstName = this.firstName;
    lastName = this.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){
    author = this.author;
    title = this.title;
    description = this.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(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);
  }

}

1 Answer

andren
andren
28,558 Points

Because you are setting topic (the parameter) equal to topic the field variable (which has not been initialized with any data) in other words you have the assignments backwards. If you reverse the order like this:

this.topic = topic;

That should clear the null-pointer error.

Edit:

I notice that you are doing the same thing in all of the other constructors, so you will have to reverse the order in the other constructors as well.