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 trialjustimagine
2,914 PointsInvocationTargetException caught
this is my first time getting this type of error, i have no idea how to go about resolving it. please help
public class Spaceship {
public Spaceship spaceShip;
public String shipType;
public Spaceship(){
shipType ="SHUTTLE";
}
public Spaceship(String shipType){
shipType = spaceShip.getShipType();
}
public String getShipType() {
return shipType;
}
public void setShipType(String shipType) {
this.shipType = shipType;
}
}
1 Answer
Tom Achki
3,680 Pointspublic Spaceship(String shipType){
this.shipType = shipType;
}
You just have to assign shipType variable to the new String shipType that was passed in the constructor. Use this.shipType to tell compiler that you are talking about original shipType(NOT the one that was passed in constructor) and assign it to the new shipType.
If this.shipType is confusing, you can also do it this way also
public Spaceship(Strings newShipType){
shipType = newShipType;
}