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

Not sure why my code doesn't work.

We are currently in the process of building a Forum. There is some skeleton code here, but I need your help to finish it up. Let's do this!

In the Forum class, add a constructor that takes a String for the topic and sets the private field mTopic.

My code is:

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

With this code I get the following message: Bummer! Did you add a new constructor that takes accepts a string?

[MOD: added ```java formatting -cf]

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The error is caused by the keyword void. When present, void signifies this function as a method instead of a constructor.

public Forum (String topic){
      mTopic = topic;
    }
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The code in my answer is correct if applied to Forum.java. If applied mistakenly to ForumPost.java, then you will get the "return type required" error because a method called "Forum" in ForumPost.java can not be a constructor due to the name difference, so the compiler sees it as a regular method that is missing the return type.

Hi, Thank you. I tried the code. Here's the error message I received:

./ForumPost.java:14: error: invalid method declaration; return type required public Forum (String topic){ ^ ./ForumPost.java:15: error: cannot find symbol mTopic = topic; } ^ symbol: variable mTopic location: class ForumPost 2 errors

This is the first challenge that has me stumped. Any suggestions on what's wrong?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

I had the same error when debugging your code. I had posted this change to ForumPost.java instead of Forum.java! :grin:

Do I understand your response to mean that there is a systemic problem and that the code was correct? Thank you for your help!

It's embarrassing how I badly misread your comment. Thank you. It works perfectly.