EditText not able to fetch data, giving null string

The problem is (in FirstPage.java) getText() is reading a null string from EditText and, not the value I am entering. As soon as the app starts i.e. the FirstPage activity begins then the Edit Text is capturing null string and then whatever I enter afterwards in that field it is not considered. And then when button named click is pressed, the null string is only captured and hence giving NumberFormat Exception always. How to solve this?

Code: ( FirstPage.java )

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_page);

        Click = findViewById(R.id.click);
        Text = findViewById(R.id.text);
        try {
            number = Integer.parseInt(Text.getText().toString());
        }catch (NumberFormatException e){
                   number = 2; //the problem is here getText() is always getting null string
                   //and hence catch statement is always getting executed
        }

        Click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ii= new Intent(FirstPage.this, MainActivity.class);
                ii.putExtra("value", number);
                startActivity(ii);
            }
        });
    }

XML code for FirstPage.java :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FirstPage">
    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:hint="Enter no of ques"
        android:layout_marginTop="30dp"/>
    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Click"/>
</LinearLayout>

MainActivity.class code part:

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            value = bundle.getInt("value");
        }

I am not able to understand what exactly I am doing wrong, please help. Thanks for your help in advance.

Answer

You are writing it in a wrong way. The try-catch block must be inside setOnClickListener because the String is getting used only when button is pressed. So you must write it in this way.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_page);

    Click = findViewById(R.id.click);
    Text = findViewById(R.id.text);
   
    Click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                 number = Integer.parseInt(Text.getText().toString());
            } catch (NumberFormatException e){
                 number = 2; 
            }
            Intent ii= new Intent(FirstPage.this, MainActivity.class);
            ii.putExtra("value", number);
            startActivity(ii);
        }
    });
}