I'm a beginner trying out Android. I've gotten hold of a several tutorials but cannot get past the first steps in all of them. I get NullPointerExceptions in the first steps as soon as I try adding a listener to a button.

For example in a Lynda android beginner tutorial the first two coding steps are as follows:

1 Add a button to the main activity.

```
Button b = (Button) findViewById(R.id.button1);

```

It runs fine

2 Add a listener to the button

```
b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

```

I get a NullPointerException error

All of the code and error is below. Outside of the code above all other code was created by default when I created the Android Application Project.

Main.java:

```
package com.example.lyndatutlearnactivites;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class Main extends ActionBarActivity {

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

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

```

R.java contains the button reference:

```
  public static final class id {
    ....
        public static final int button1=0x7f05003d;
    ....
    }

```

Error:

```
03-31 19:39:16.375: D/AndroidRuntime(1978): Shutting down VM
03-31 19:39:16.375: W/dalvikvm(1978): threadid=1: thread exiting with uncaught exception (group=0xb2d49b20)
03-31 19:39:16.445: E/AndroidRuntime(1978): FATAL EXCEPTION: main
03-31 19:39:16.445: E/AndroidRuntime(1978): Process: com.example.lyndatutlearnactivites, PID: 1978
03-31 19:39:16.445: E/AndroidRuntime(1978): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lyndatutlearnactivites/com.example.lyndatutlearnactivites.Main}: java.lang.NullPointerException
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.os.Looper.loop(Looper.java:136)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at java.lang.reflect.Method.invoke(Method.java:515)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at dalvik.system.NativeStart.main(Native Method)
03-31 19:39:16.445: E/AndroidRuntime(1978): Caused by: java.lang.NullPointerException
03-31 19:39:16.445: E/AndroidRuntime(1978):     at com.example.lyndatutlearnactivites.Main.onCreate(Main.java:24)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.Activity.performCreate(Activity.java:5231)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-31 19:39:16.445: E/AndroidRuntime(1978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-31 19:39:16.445: E/AndroidRuntime(1978):     ... 11 more

```

Line 24 is the:

```
b.setOnClickListener(new OnClickListener() {

```

activity\_mail.xml is:

```
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lyndatutlearnactivites.Main"
    tools:ignore="MergeRootFrame" />

```

There is also a file called fragment\_main.xml with:

```
<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.lyndatutlearnactivites.Main$PlaceholderFragment" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

```

The tutorials are built on API level 10 and my ADT eclipse builds on level 19 so I don't know if that makes a difference. All the extra code that is added at level 19 is confusing.
What is causing the error?