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

Android Build a Simple Android App (2014) Improving Our Code Creating a Class

vince hoyt
vince hoyt
1,423 Points

Missing return statement

I keep getting a missing return statement error, and if i ad a return statement it I get an illegal start error and pointing to the "r" in return. What am i doing wrong?

PictureBook.java
public class PictureBook 
{
  public String mTitle()
  {

  } 
}

3 Answers

Stone Preston
Stone Preston
42,016 Points

the task states: Now add a String member variable, name it mTitle make it public.

you added a method, not a variable. to declare a member variable, simply provide the type and the name for the variable (member variables conventionally start with m), followed by a semicolon. dont forget to make it public:

public class PictureBook{
  public String mTitle;
}

methods have a return type, a name, and a parameter list that goes in between parenthesis.

public String mTitle() {   
  }

the code above creates a method called mTitle that returns a string and takes no arguments. the task asked for a variable, not a method, which is why the above code is incorrect.

Daniel Barreto
PLUS
Daniel Barreto
Courses Plus Student 14,978 Points

Can you show me what you are writing as the answer?

It is not enough to simply put return. Something, a String in this case, needs to be returned.

So if you have a String variable named myString, the way you return it is:

public class PictureBook 
{
  public String mTitle()
  {
      return myString;
  } 
}

Also, that variable needs to have a String in it.

You should Return a some kind of String and it should work.

public String mTitle(){

return "title";

}

After that you can assign your returned string to any variable for example ;

String returnedTitle = mTitle();

Have a nice coding...

Stone Preston
Stone Preston
42,016 Points

the task is expecting a member variable not a method.