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 trialKantimoy Sur
iOS Development with Swift Techdegree Student 22,499 PointsChallenge Task 4 of 4 in Java Objects
Everything seems to be OK, but i am getting an error . Actually i am getting an error for last question.
Starting forum example...
java.lang.NullPointerException
at Forum.addPost(Forum.java:15)
at Example.main(Example.java:15)
at JavaTester.run(JavaTester.java:183)
at JavaTester.main(JavaTester.java:43)
Can you suggest how to remove the error for NullPointerException ?
public class ForumPost {
private User mAuthor;
private String mTitle;
private String mDescription;
public User getAuthor() {
return mAuthor;
}
public ForumPost(User author, String title, String description) {
mTitle = title ;
mDescription = description ;
}
public String getTitle() {
return mTitle;
}
public String getDescription() {
return mDescription;
}
// 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 Topic){
mTopic = Topic ;
}
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(args[0], args[1]);
//author(args[0], args[1]);
// Add the author, title and description
ForumPost post = new ForumPost(author, args[0],args[1]);
//post(author, "Kantimoy","amazing");
forum.addPost(post);
}
}
2 Answers
samuel zaffran
24,815 PointsHi,
I think the problem come from here :
public ForumPost(User author, String title, String description) {
mTitle = title ;
mDescription = description ;
}
I let you think about it, you almost get it ! Something missing, you set title and description already.. Luck.
Jordan Ernst
5,121 Pointsthe hint is in the instructions, they introduced a bug in your code. i realized this when reading through the lines i noticed what i had set in my ForumPost constructor was gone. you will see the error and finally be on your way the guy before me is right!
Jordan Ernst
5,121 PointsJordan Ernst
5,121 Pointsi am having the exact same problem let me know if you get this resolved i did this challenge 3 times and it just says oops. we encountered a problem after telling me there is a null pointer.