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

App crashes

I thought I copied everything in the videos but when ever I run this code i receive a message (in the emulator) : "unfortunately InteractiveStory has stopped working". After i get the message, the app (named InteractiveStory) crashes! Please view the code and help me figure out what the problem is. Thanks in advance!

-----------CODE STARTS HERE-----------------------------------

package amitkamma.interactivestory;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText mNameField;
private Button mStartButton;

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

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

}

---CODE ENDS HERE---------

Can you tell us what is says in the log/console?

2 Answers

The problem lies within the Toast message. I went ahead and recreated the same app from scratch without the Toast message and it worked fine. I just proceeded to the next tutorial without the Toast due to the fact that on the next tutorial your going to delete the Toast Anyways. But for more info on alternative ways to implement a Toast message to the user, you can always youtube it and implement it when needed. I made sure not to waste to much time here because for some reason i just couldn't get it to work with the Toast message provided. Hope this helps.

The top of the console says this:

FATAL EXCEPTION: main Process: amitkamma.interactivestory, PID: 2806 java.lang.RuntimeException: Unable to start activity ComponentInfo{amitkamma.interactivestory/amitkamma.interactivestory.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference