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 an Interactive Story App (Retired) The Model-View-Controller Pattern Adding Custom Constructors

Orla McGreal
Orla McGreal
1,625 Points

I keep getting this error : java.lang.NullPointerException at JavaTester.run(JavaTester.java:1

In the code challenge I keep getting this error

java.lang.NullPointerException at JavaTester.run(JavaTester.java:100) at JavaTester.main(JavaTester.java:39)

This is my code - could anyone show me where I am going wrong - I don't really understand the task....

public class Spaceship { public String mType; public String mShuttle;

public Spaceship (){ mType = "SHUTTLE"; }

public Spaceship (String MShuttle){ mShuttle ="Apollo 13"; }

Thanks so much in advance!!

1 Answer

You second constructor needs to set

mType

to the the argument it receives. e.g.

public Spaceship (String type)
{
  mType = type;
}

The null pointer exception is likely causes by the test code calling your second construction and then trying to access mType to ensure it has been set. Since mType is still null it fails.

Orla McGreal
Orla McGreal
1,625 Points

Thank you so much it's beginning to all make sense!!!