Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Mario Castro
Mario Castro
5,369 Points

I can't pass a parameter to the constructor of another class.

Hi Guys!!

How you doing?

I am working on the penultimate code challengue of the Java Objects course and in the fourth part of the challengue, i have to pass 3 parameters from the Main class to the ForumPost class and i am getting the following error with the parameter author (look below):

./ForumPost.java:10: error: incompatible types: String cannot be converted to User author = "Mario Castro";

------ The way in which I am passing the parameters (from main class to forumpost class) is the following.

°°° Main.java

ForumPost post = new ForumPost(author, "creator of programs", "A simple introduction"); forum.addPost(post);

------ The ForumPost class has three private variables and the following constructor:

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 = "Mario Castro"; title = "creator of programs"; description = "A simple introduction"; }

--------> As I said above the issue is with the paramether named author that as you may see is of type user, and i can't figure out how to pass it because everytime that i try to compile the code I keep getting the same error.

Any help that you may provide, will be appreciated. If something is not clear, please let me know.

Thank you for your attention to this matter

2 Answers

andren
andren
28,538 Points

So If I understand you right the constuctor for the ForumPost class looks like this:

public ForumPost(User author, String title, String description) {
    author = "Mario Castro";
    title = "creator of programs";
    description = "A simple introduction"; 
}

If that is the case then that would be at least one point of error, the constructor is meant to take the parameters passed in (author, title, description) and assign the value of them to the field variables of the same name. Your code does not do that, your code takes the parameters variables and overrides their values with hardcoded string values. The fact that you assign a string to author which is declared as a User object is what generates an error, but that is only part of the problem. The proper code for the ForumPost constructor for this challenge looks like this:

public ForumPost(User author, String title, String description) {
    this.author = author;
    this.title = title;
    this.description = description;
}

The above code will assign the content of the parameters to the field variables, since the field variables are named the same as the constructor parameters you have to prepend them with the this keyword.

Changing the constructor as shown above should fix the current error you get but something tells me it likely isn't the only error in your code. I would take a look at the other constructors you have written and make sure they conform to the above format as well.

Without seeing all your code this is pretty much all the advice I can give you.

Mario Castro
Mario Castro
5,369 Points

Hi Andren,

Thanks for answering.

In my previous post, i guess i did not explain well my doubt (it's my fault). Actually my question is:

  • how can i pass the parameter/variable (author) of type User.java to a constructor in a different class?

When i ask this, what i mean is that i understand when it's passed as a parameter a string, int, char and so on but, i do not still understand how to pass author of type User.

In case is needed, i can copy all the classes that i have typed in the challengue in order to verify the methods.

(My apologies if my questions are dumb, I am a beginner) I appreciate your help in this.

andren
andren
28,538 Points

The code you had for passing the author variable was not wrong:

ForumPost post = new ForumPost(author, "creator of programs", "A simple introduction");

Custom classes like User are not any different from String, int or char when it comes to passing them around.

If author does in fact contain a User object then the above code is correct, it will pass the User object stored in the author variable to the ForumPost constructor correctly, as long as the ForumPost constructor is actually set up correctly to assign the parameters passed in to the field variables. In the code you posted it was not setup correctly. That is why I concentrated on that aspect in my post.

As I said in my previous post I can't really give any more advice than I have without seeing more of your code and seeing the error messages you are getting. If you fix the ForumPost constructor like I showed above then you should not receive the same error message you got before. If you still receive an error it would help me to know exactly what the error message actually states.

Mario Castro
Mario Castro
5,369 Points

Hi Andren!

Thank you for your help.

Your explanation helped me to better understand the code.

Have a great day!!