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

Miguel Frey
686 Points.java:15: error: missing return statement
Hi, I'm having some troubles on the Teacher Assistant exercise, here's my code
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
// These things should be verified:
char ch1 = fieldName.charAt(0);
char ch2 = fieldName.charAt(1);
if((ch1=='m')){ // 1. Member fields must start with an 'm'
if(Character.isUpperCase(ch2)){ // 2.The second letter in the field name must be uppercased to ensure camel-casing
return fieldName;
}
}
else {
throw new IllegalArgumentException("doesn't meet the requirements");
}
}
}
and this is the error that comes up when compiling
./TeacherAssistant.java:15: error: missing return statement } ^ 1 error I've checked on Google and this was the answer I found out.
"Provide a return statement after the end of the while loop, or throw some kind of an Exception (IllegalStateException?) if the code really shouldn't ever make it there."
I threw an exception so what am I doing wrong?
Thanks for any further help
2 Answers

Derek Markman
16,291 PointsIt is giving you that error message because you need to add an else clause to both of your if statements.
I refactored a little bit but it works as expected:
public class TeacherAssistant {
private static char char1;
private static char char2;
private static String mFieldName;
public static void main(String[] args) {
validateFieldName("mName");
System.out.println("validation successful");//will only run if no exception is thrown
System.out.println("The fieldName was: [" + mFieldName + "]");//will only run if no exception is thrown
}
public static String validateFieldName(String fieldName) {
char1 = fieldName.charAt(0);
char2 = fieldName.charAt(1);
mFieldName = fieldName;
if(char1 == 'm') {
if(Character.isUpperCase(char2)) {
return mFieldName;
} else {
throw new IllegalArgumentException("SECOND CHAR IS NOT UPPERCASE");
}
}
else {
throw new IllegalArgumentException("DOESN'T START WITH A 'm'");
}
}
}

Miguel Frey
686 Pointsoh good point, thanks for your help :)

Derek Markman
16,291 PointsNo problem.