I am new to android.I have written code for Sum of two integers. I am getting NULLPointerException. findViewById() method returns null. Can someone help me to fix the error.
    <EditText
        android:id="@+id/firstInt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:hint="@string/InputHint"
        />
    <EditText
        android:id="@+id/secondInt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:hint="@string/InputHint"
        />
    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:textSize="18dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:layout_gravity="center"
        android:onClick="calculateAdd"/>
MainActivity.java:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText e1 = (EditText)findViewById(R.id.firstInt);
        EditText e2 = (EditText)findViewById(R.id.secondInt);
        TextView tv = (TextView)findViewById(R.id.result);
    }
    public void calculateAdd(View view){
            int x = Integer.parseInt(e1.getText().toString());
            int y = Integer.parseInt(e2.getText().toString());
            int z = x + y;
        tv.setText(z);
    }
It is a very simple program I have written. But, facing the issue.
My stack trace error:
java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
 
    