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) Finishing the User Interface Formatting Strings

jinhwa yoo
jinhwa yoo
10,042 Points

helpme appisnotworking

whatswrong???

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

package teamtreehouse.com.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import teamtreehouse.com.interactivestory.R; import teamtreehouse.com.interactivestory.model.Page; import teamtreehouse.com.interactivestory.model.Story;

public class StoryActivity extends Activity { public static final String TAG = StoryActivity.class.getSimpleName(); private Story mStory = new Story(); private ImageView mImageView; private TextView mTextView; private Button mChoice1; private Button mChoice2; private String mName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);
    mStory = new Story();
    Intent intent = getIntent();
    String mName = intent.getStringExtra(getString(R.string.key_name));
    if (mName == null) {
        mName = "Friends";
    }
    Log.d(TAG, mName);
    mImageView = (ImageView)findViewById(R.id.StoryImageView);
    mTextView = (TextView)findViewById(R.id.textView);
    mChoice1 = (Button)findViewById(R.id.choiceButton1);
    mChoice2 = (Button)findViewById(R.id.choicebutton2);

    loadPage();
}
private void loadPage() {
    Page page = mStory.getPage(0);
    Drawable drawable = getResources().getDrawable(page.getImageId());
    mImageView.setImageDrawable(drawable);

    String pageText = page.getText();
    //Add the name if placeholder inclueded!! won't add if no placeholder//
    pageText = String.format(pageText, mName);
    mTextView.setText(pageText);
    mChoice1.setText(page.getChoice1().getText());
    mChoice2.setText(page.getChoice2().getText());
}

}

Seth Kroger
Seth Kroger
56,413 Points

Can you post your code. It looks like you didn't set a TextView variable correctly before using it.

Seth Kroger
Seth Kroger
56,413 Points

Okay, in the layout file can you check that the id of the TextView is the same as the one used in the Java file to set mTextView? Same for the buttons.

2 Answers

jinhwa yoo
jinhwa yoo
10,042 Points

I solved the problem... thanks...

jinhwa yoo
jinhwa yoo
10,042 Points

hey I have a question about this code.. import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;

public class LandingActivity extends Activity {

public Button mThrustButton;
public TextView mTypeLabel;
public EditText mPassengersField;

public Spaceship mSpaceship;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_landing);

    mThrustButton = (Button)findViewById(R.id.thrustButton);
    mTypeLabel = (TextView)findViewById(R.id.typeTextView);
    mPassengersField = (EditText)findViewById(R.id.passengersEditText);

    mSpaceship = new Spaceship("FIREFLY");
    mTypeLabel.setText(mSpaceship.getType());
}

}

-> update the TextView to use the Spaceship's type property. Remember to use the helpful getter method!

if i want to update the TextView mTypeLabel to use the Spaceship's type property, why do i have to put "setText" method? where is the setText coming from??