Question: When I do not enter anything in the EditText and then I press the "Guess" button, my app keeps crashing and I can't seem to find the problem with my code.

```
public void generateRandomNos(){
      Random rand = new Random();
      randomNumber = rand.nextInt(20)+1;
}

btnGuess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int guessValue = Integer.parseInt(etNumber.getText().toString());
                String message;

                if(guessValue > randomNumber){
                    message = "Lower !";
                    etNumber.setText("");
                }
                else if (guessValue < randomNumber){
                    message = "Higher !";
                    etNumber.setText("");
                }
                else{
                    message = "You got it right. GG ";
                    etNumber.setText("");
                    generateRandomNos();
                }

                Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
            }
});

```

The app should not crash when there is no text in the `EditText` box and when the button is pressed. What could be the problem?

Answer: 
As you mentioned in your question that, this error occurring when you don't enter anything in the edit text.

So first, check if the `edit text` is empty or not and then perform the required action 

```
btnGuess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

       if(!etNumber.getText.toString().equals("")) {           
         int guessValue =  Integer.parseInt(etNumber.getText().toString());

                String message;

                if(guessValue > randomNumber){
                    message = "Lower !";
                    etNumber.setText("");
                }
                else if (guessValue < randomNumber){
                    message = "Higher !";
                    etNumber.setText("");
                }
                else{
                    message = "You got it right. GG ";
                    etNumber.setText("");
                    generateRandomNos();
                }

        Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
            }
         } else {
           Toast.makeText(MainActivity.this,"EditText Is empty",Toast.LENGTH_SHORT).show();
         }
   });

```

> 
> **Note:** And Make sure you use `android:inputType="number"` attribute for
>  edit text so that you can get only numbers as input
> 
> 
>
Of course it will be crash because you never assigned this method `generateRandomNos()` before Click event. So your application didn't know what is value of `randomNumber` before trigger of event. What you can do now is to get value of `randomNumber` before perform btnGuess clicked. For example 

```
btnGuess.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                        generateRandomNos();  // i change this position
                    int guessValue = 
    Integer.parseInt(etNumber.getText().toString());
                    String message;

                    if(guessValue > randomNumber){
                        message = "Lower !";
                        etNumber.setText("");
                    }
                    else if (guessValue < randomNumber){
                        message = "Higher !";
                        etNumber.setText("");
                    }
                    else{
                        message = "You got it right. GG ";
                        etNumber.setText("");
                    }

```