title: AndroidRuntime error at com.example.appname.MainActivity.onCreate
body: <p>I am trying to write Java code that creates a button click event that populates an edit text box with my name. Unfortunately I am getting runtime errors. I checked almost all related topics and questions on stackoverflow but couldn't figure it out my problem.
  Appreciated for your helps.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.question5;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.*;
  import android.widget.*;
  
  public class MainActivity extends Activity {
      Button b;
      EditText et;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          et=(EditText)findViewById(R.id.editText1);
          b=(Button)findViewById(R.id.button1);
          b.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  et.setText("My Name");          
              }
          });     
      }
  }
  </code></pre>
  
  <p>fragment_main.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.question5.MainActivity$PlaceholderFragment" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="178dp"
          android:text="Populate" /&gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBottom="@+id/button1"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="54dp"
          android:ems="10"
          android:inputType="textPersonName" &gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.question5"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.question5.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Error LogCat</p>
  
  <pre><code>03-21 23:38:26.227: W/dalvikvm(28343): threadid=1: thread exiting with uncaught exception (group=0x40bfd1f8)
  03-21 23:38:26.227: E/AndroidRuntime(28343): FATAL EXCEPTION: main
  03-21 23:38:26.227: E/AndroidRuntime(28343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.question5/com.example.question5.MainActivity}: java.lang.NullPointerException
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.os.Handler.dispatchMessage(Handler.java:99)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.os.Looper.loop(Looper.java:137)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread.main(ActivityThread.java:4514)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at dalvik.system.NativeStart.main(Native Method)
  03-21 23:38:26.227: E/AndroidRuntime(28343): Caused by: java.lang.NullPointerException
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at com.example.question5.MainActivity.onCreate(MainActivity.java:19)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.Activity.performCreate(Activity.java:4465)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
  03-21 23:38:26.227: E/AndroidRuntime(28343):    ... 11 more
  </code></pre>