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) User Input Getting Text from an EditText

Crashing my app

when running my app at the end, after I input my name and then click on the button my app crashes. I commented out the toast thinking that was the problem. It still crashes. I then restarted my emulator and ran it again with debug on and got the following in logcat: 03-06 17:55:43.900 937-937/com.example.darthhiggy.interactivesotry E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.darthhiggy.interactivesotry, PID: 937 java.lang.NullPointerException at com.example.darthhiggy.interactivesotry.MainActivity$1.onClick(MainActivity.java:28) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

my code looks like this:

public class MainActivity extends Activity {

private EditText mNameField;
private Button mStartButton;

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

    mStartButton = (Button)findViewById(R.id.startButton);

    mStartButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String name = mNameField.getText().toString();
           // Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
        }
    });
}

2 Answers

Víctor Hernández
Víctor Hernández
12,276 Points

Add the next line after the initialization of mSartButton

mNameField = (EditText) findViewById(R.id.nameField);

hi, i have the same problem with this code

package componente.com.historia;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText mNameField;
    private Button mStartButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main); // You need to have this line


        mNameField = (EditText) findViewById(R.id.nameEditText);
        mStartButton = (Button) findViewById(R.id.StartButton);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = mNameField.getText().toString();
                Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();
            }
        });
    }
}

any idea?