NullPointerException when calling GetText()

First off, I'm an Android noob and I'm making a simple app that takes text from an EditText widget and shows it in a TextView when you tap on a button. But when the button is clicked, it forcecloses and I get a NullPointerException in the logcat.

My activity code is:

package com.deltablue.freeearl.ama.ama;

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


public class MainActivity extends Activity {

    private Button b1;
    private EditText e1;
    private TextView t1;

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

        //Definiciones de los elementos
        Button b1 = (Button) findViewById(R.id.bttn1);
        EditText e1 = (EditText) findViewById(R.id.edit1);
        TextView t1 = (TextView) findViewById(R.id.txt1);
    }

    public void b1click(View view) {
        Toast.makeText(this, "Pressed", Toast.LENGTH_SHORT).show();
        String text = e1.getText().toString();
        t1.setText("Input: ");
    }
}

The thing is, when debugging, the findViewById() calls return correct values. But when b1click() is executed, e1 & t1 are null. All the other similar questions I've seen have to see with findViewById() returning null or wrong widget IDs, but that's not my case, AFAIK. I know it probably is some really stupid error, but I don't see where I'm wrong.

My layout is:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.deltablue.freeearl.ama.ama.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show text"
            android:id="@+id/bttn1"
            android:layout_gravity="center_horizontal"
            android:onClick="b1click" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/txt1"
            android:editable="true" />
    </LinearLayout>
</RelativeLayout>

Thanks in advance for the help!

Jon Skeet
people
quotationmark

Here, in onCreate:

Button b1 = (Button) findViewById(R.id.bttn1);
EditText e1 = (EditText) findViewById(R.id.edit1);
TextView t1 = (TextView) findViewById(R.id.txt1);

... you're declaring three local variables. Those are completely separate from the fields that you declared earlier:

private Button b1;
private EditText e1;
private TextView t1;

I'm pretty sure you just wanted to change the values of the fields instead, so you just want to assign new values to the variables, rather than redeclaring them:

b1 = (Button) findViewById(R.id.bttn1);
e1 = (EditText) findViewById(R.id.edit1);
t1 = (TextView) findViewById(R.id.txt1);

people

See more on this question at Stackoverflow