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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Grigorij Schleifer](https://uploads.teamtreehouse.com/production/profile-photos/3207582/micro_wallhaven-15652.jpg)
Grigorij Schleifer
10,365 PointsFATAL EXCEPTION
Dear community,
My plan is to type a text into a TextView and display this text in a different activity´s TextView.
I get this nasty error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nexuskiller.interactivestory/com.example.nexuskiller.interactivestory.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
This is my code:
MainActivity
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView = (TextView) findViewById(R.id.textView);
Button mButton = (Button) findViewById(R.id.button);
private String mText = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mText = mTextView.getText().toString();
callNewActivity(mText);
}
});
}
public void callNewActivity(String text) {
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(getString(R.string.name), text);
startActivity(intent);
}
}
secondActivity
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class secondActivity extends Activity {
public static final String TAG = secondActivity.class.getSimpleName();
TextView secondTW = (TextView) findViewById(R.id.secondEditText);
String stringFromTW = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
stringFromTW = intent.getStringExtra(getString(R.string.name));
secondTW.setText(stringFromTW);
}
}
Any help is highly appreciated :)
Grigorij
1 Answer
![Kourosh Raeen](https://uploads.teamtreehouse.com/production/profile-photos/3971082/micro_TreehouseProfilePic.jpg)
Kourosh Raeen
23,733 PointsHi Grigorij - The calls to findViewById need to be inside onCreate() and after the call to setContentView(), so your first activity should look like this:
package com.example.nexuskiller.interactivestory;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
private String mText = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mText = mTextView.getText().toString();
callNewActivity(mText);
}
});
}
public void callNewActivity(String text) {
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra(getString(R.string.name), text);
startActivity(intent);
}
}
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi Kourosh,
thank you sooo much. You saved my head from turning into a giant black hole![:smiley: :smiley:](https://image-proxy-cdn.teamtreehouse.com/f266e5f075421fb8b3a44f1b8d72534e51ae5854/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f31663630332e706e67)
Grigorij