0

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

1 Answers1

8

You are just Declaring the EditText inside the Scope of onCreate() method.

EditText e1, e2;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    e1 = (EditText)findViewById(R.id.firstInt);
    e2 = (EditText)findViewById(R.id.secondInt);
    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);
}  

May this helps you.

if you need more information Please refer to this link https://stackoverflow.com/a/51688200/5343866

Abhinav Suman
  • 940
  • 1
  • 9
  • 29
  • 1
    As a simple explanation, declaring a variable inside of a method, will make them `private` for that method only . You cannot use them outside of it. That's why he declared them `global` in the class, initialize in `onCreate` and used in `calculateAdd` – Ionut J. Bejan Sep 13 '18 at 14:41
  • Don't post same answer https://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplicate-answer-to-several-questions – AskNilesh Sep 14 '18 at 04:14
  • 1
    @Abhinav Suman, Thanks. It solved my problem. I don't know how did I missed it. Thanks a lot for your help. – Shankar Muthu samy Sep 18 '18 at 04:58