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 trialJoseph Collins
802 PointsConstructor with input string
Hello, I'm having trouble with one of the exercises. I've made the first constructor which defines mType as "SHUTTLE".
But I'm stuck on the second part of the question which says "Using a second constructor, with a string, update mType to read a different type of spaceship.
I've tried a number of things, this one looked the closest but I get a java.lang.nullexception error. I'm sure I'm missing something really obvious but I can't find it. Thanks very much
public class Spaceship {
public String mType;
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
public Spaceship() {
mType = "SHUTTLE";
}
public Spaceship(String mType) {
mType = "ROCKET";
}
}
1 Answer
Zack Klinger
17,622 PointsDon't name the String variable you are passing "mType" which is the same name as your private member variable, "mType".
try something like, public Spaceship (String to_set)
and then it would look like mType = to_set;
Joseph Collins
802 PointsJoseph Collins
802 PointsAh, I understand now. The exercise is about defining the constructors, and the second example is for when the constructor is called with a string.
Cheers Zack!