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

Null Pointer Exception Question

I am trying to pass an intent and I have done exactly what Ben did in his tutorial but I am getting this error. I have tried using tips from a similar problem on stack overflow but to no avail.

The first error is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.calcwork.michael.math151hw/com.calcwork.michael.math151hw.UI.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(float)' on a null object reference

The second error is:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(float)' on a null object reference at com.calcwork.michael.math151hw.UI.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:25)

My code is:

public class MainActivity extends ActionBarActivity {

private Button mButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton2 = (Button) findViewById(R.id.button2);

    mButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = "2e^2x";
            startStory(name);
        }
    });
}

private void startStory(String name) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    intent.putExtra("name", name);
    startActivity(intent);
}
}

and the second activity:

public class DisplayMessageActivity extends ActionBarActivity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String mName = intent.getStringExtra("message");

    // Create the text view
    textView = (TextView)findViewById(R.id.textView);
    textView.setText(mName);
}

}

2 Answers

Hi Micheal

It looks as if your missing your call to setContentView() in your DisplayMessageActivity class.

Try adding the line as necessary after the line super.onCreate(savedInstanceState); and see how you fare.

Hope this helps Daniel

I used setContentView(textView) and I got this error after pressing the button in the emulator:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.calcwork.michael.math151hw.UI.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:25)

Hi Michael

The call to setContentView() should contain the path of the layout (xml) file you are referring to so the program knows which of the textViews you mean so to speak.

You did this in your initial class using the line

setContentView(R.layout.activity_main);

I assume the layout you are referring to is the same one but without knowing your entire structure I can not be sure

Thanks Daniel

I changed it to what you said I should, and I still am getting a Null Pointer Exception :(

When I am running the app and click the button, it says on the emulator that the app "stopped responding".

I put setContentView before setting mButton 2 and I finally got it to work. Thanks for the help Daniel!