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 - Unfortunately, Interactive Story has stopped

Here's the log...

03-23 17:27:29.176 1945-1945/centralservicesmobile.com.interactivestory I/art﹕ Not late-enabling -Xcheck:jni (already on) 03-23 17:27:29.399 1945-1963/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Render dirty regions requested: true 03-23 17:27:29.399 1945-1945/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c35280, tid 1945 03-23 17:27:29.413 1945-1945/centralservicesmobile.com.interactivestory D/Atlas﹕ Validating map... 03-23 17:27:29.476 1945-1963/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c35320, tid 1963 03-23 17:27:29.487 1945-1963/centralservicesmobile.com.interactivestory I/OpenGLRenderer﹕ Initialized EGL, version 1.4 03-23 17:27:29.530 1945-1963/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Enabling debug mode 0 03-23 17:27:29.551 1945-1963/centralservicesmobile.com.interactivestory W/EGL_emulation﹕ eglSurfaceAttrib not implemented 03-23 17:27:29.551 1945-1963/centralservicesmobile.com.interactivestory W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c6cd80, error=EGL_SUCCESS 03-23 17:27:35.414 1945-1963/centralservicesmobile.com.interactivestory W/EGL_emulation﹕ eglSurfaceAttrib not implemented 03-23 17:27:35.414 1945-1963/centralservicesmobile.com.interactivestory W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c6cd80, error=EGL_SUCCESS 03-23 17:30:35.051 1945-1945/centralservicesmobile.com.interactivestory D/StoryActivity﹕ max 03-23 17:30:35.061 1945-1945/centralservicesmobile.com.interactivestory D/AndroidRuntime﹕ Shutting down VM 03-23 17:30:35.061 1945-1945/centralservicesmobile.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: centralservicesmobile.com.interactivestory, PID: 1945 java.lang.RuntimeException: Unable to start activity ComponentInfo{centralservicesmobile.com.interactivestory/centralservicesmobile.com.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at centralservicesmobile.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at centralservicesmobile.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 03-23 17:45:20.867 2374-2386/centralservicesmobile.com.interactivestory I/art﹕ Background partial concurrent mark sweep GC freed 84(16KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 5MB/9MB, paused 14.503ms total 37.265ms 03-23 17:45:20.909 2374-2390/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Render dirty regions requested: true 03-23 17:45:20.910 2374-2374/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c33280, tid 2374 03-23 17:45:20.916 2374-2374/centralservicesmobile.com.interactivestory D/Atlas﹕ Validating map... 03-23 17:45:20.955 2374-2390/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c33560, tid 2390 03-23 17:45:20.976 2374-2390/centralservicesmobile.com.interactivestory I/OpenGLRenderer﹕ Initialized EGL, version 1.4 03-23 17:45:20.996 2374-2390/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Enabling debug mode 0 03-23 17:45:21.007 2374-2390/centralservicesmobile.com.interactivestory W/EGL_emulation﹕ eglSurfaceAttrib not implemented 03-23 17:45:21.007 2374-2390/centralservicesmobile.com.interactivestory W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c5afa0, error=EGL_SUCCESS 03-23 17:45:21.584 2374-2374/centralservicesmobile.com.interactivestory I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread. 03-23 17:45:24.863 2374-2374/centralservicesmobile.com.interactivestory D/StoryActivity﹕ sd 03-23 17:45:24.873 2374-2374/centralservicesmobile.com.interactivestory D/AndroidRuntime﹕ Shutting down VM 03-23 17:45:24.873 2374-2374/centralservicesmobile.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: centralservicesmobile.com.interactivestory, PID: 2374 java.lang.RuntimeException: Unable to start activity ComponentInfo{centralservicesmobile.com.interactivestory/centralservicesmobile.com.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at centralservicesmobile.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at centralservicesmobile.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 03-23 17:46:05.008 2398-2413/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Render dirty regions requested: true 03-23 17:46:05.009 2398-2398/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c21280, tid 2398 03-23 17:46:05.015 2398-2398/centralservicesmobile.com.interactivestory D/Atlas﹕ Validating map... 03-23 17:46:05.119 2398-2413/centralservicesmobile.com.interactivestory D/﹕ HostConnection::get() New Host Connection established 0xa6c21330, tid 2413 03-23 17:46:05.125 2398-2413/centralservicesmobile.com.interactivestory I/OpenGLRenderer﹕ Initialized EGL, version 1.4 03-23 17:46:05.138 2398-2413/centralservicesmobile.com.interactivestory D/OpenGLRenderer﹕ Enabling debug mode 0 03-23 17:46:05.145 2398-2413/centralservicesmobile.com.interactivestory W/EGL_emulation﹕ eglSurfaceAttrib not implemented 03-23 17:46:05.146 2398-2413/centralservicesmobile.com.interactivestory W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6ca0e80, error=EGL_SUCCESS 03-23 17:46:07.053 2398-2398/centralservicesmobile.com.interactivestory D/StoryActivity﹕ [ 03-23 17:46:07.064 2398: 2398 D/AndroidRuntime ] Shutting down VM 03-23 17:46:07.064 2398-2398/centralservicesmobile.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: centralservicesmobile.com.interactivestory, PID: 2398 java.lang.RuntimeException: Unable to start activity ComponentInfo{centralservicesmobile.com.interactivestory/centralservicesmobile.com.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at centralservicesmobile.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at centralservicesmobile.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 03-23 17:50:17.335 2462-2462/centralservicesmobile.com.interactivestory D/StoryActivity﹕ fred 03-23 17:50:17.355 2462-2462/centralservicesmobile.com.interactivestory D/AndroidRuntime﹕ Shutting down VM 03-23 17:50:17.355 2462-2462/centralservicesmobile.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: centralservicesmobile.com.interactivestory, PID: 2462 java.lang.RuntimeException: Unable to start activity ComponentInfo{centralservicesmobile.com.interactivestory/centralservicesmobile.com.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at centralservicesmobile.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at centralservicesmobile.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 03-23 17:53:08.874 2506-2506/centralservicesmobile.com.interactivestory D/StoryActivity﹕ df 03-23 17:53:08.882 2506-2506/centralservicesmobile.com.interactivestory D/AndroidRuntime﹕ Shutting down VM 03-23 17:53:08.882 2506-2506/centralservicesmobile.com.interactivestory E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: centralservicesmobile.com.interactivestory, PID: 2506 java.lang.RuntimeException: Unable to start activity ComponentInfo{centralservicesmobile.com.interactivestory/centralservicesmobile.com.interactivestory.ui.StoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String centralservicesmobile.com.interactivestory.model.Choice.getText()' on a null object reference at centralservicesmobile.com.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at centralservicesmobile.com.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:46) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 03-23 17:53:10.700 2506-2506/centralservicesmobile.com.interactivestory I/Process﹕ Sending signal. PID: 2506 SIG: 9 device not found a

CodeChallenge.java
EditText petNameField = (EditText)findViewById(R.id.petNameField);
String petName;

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Fred

Not sure if you are referring to the code challenge snippet you posted at the bottom or your own app.

The code challenge snippet you posted at the bottom should be initalised like the below

EditText petNameField = (EditText)findViewById(R.id.petNameField);
String petName = petNameField.getText().toString();

The reason we have to call the method toString() after the call to getText is becuase this call to getText actually returns the Editable type and not a string the toString() method converts this Editable data into a string for you to store in your string variable. The documentation (if your interested) can be found using the link below

http://developer.android.com/reference/android/widget/EditText.html

Hope this helps

Daniel

Hi Daniel,

Thanks for the quick response. I'm a beginner so please forgive me if I seem confused. I'm working on the Android Interactive Story project. In my previous message, I pasted the entire log contents, but have never seen petNameField until you mentioned it.

Here's the MainActivity.java code from my project: package centralservicesmobile.com.interactivestory.ui;

import android.app.Activity; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;

import centralservicesmobile.com.interactivestory.R;

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);

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

    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
    public void onClick(View v) {
            String name = mNameField.getText().toString();
            startStory(name);
        }
    });
}

private void startStory(String name) {
    Intent intent = new Intent(this, StoryActivity.class);
    intent.putExtra(getString(R.string.key_name), name);
    startActivity(intent);
}

}

Thanks for any advice!

  • Fred
Daniel Hartin
Daniel Hartin
18,106 Points

Ah okay no problem Fred, the logcat seems to indicate a null pointer exception so check to make sure that the edittext and button id's are spelt exactly the same as they are in the xml file(remember they are case sensitive) secondly delete the import statements that say widget.button and widet.edittext I may not be correct but I'm pretty sure they should be extensions of the view class so when you delete the import statements and your code gets underlined in red see if you can import from View.Button and View.EditText. Like I say I may not be correct but worth a try

Let me know how you get on

Thanks Daniel