0
 public void clickFunction(View view)
    {
        Log.i("Info","Button Pressed");
        EditText e1=(EditText)findViewById(editText);
        Number myNumber= new Number();
        String NumberEnteredStr = e1.getText().toString();
        Log.i("Number Entered",NumberEnteredStr);
        myNumber.num = Integer.parseInt(NumberEnteredStr);//line 22
    }

I entered the entire code correctly bit by bit until the app crashed while I entered the code on line no. 22. There seems to be some problem with the conversion to int. Please help me out.

PS- I have tried removing different parts of the code to debug and the problem lies somewhere in the remaining portion, possibly in the clickFunction (onClick) method.

My error messages are :

    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:6294) 
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
    at android.view.View$PerformClick.run(View.java:24770) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
 Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:620)
    at java.lang.Integer.parseInt(Integer.java:643)
    at com.example.numbershapes.MainActivity.clickFunction(MainActivity.java:22)

I am putting down my entire code for reference :

package com.example.numbershapes;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import static com.example.numbershapes.R.id.editText;

public class MainActivity extends AppCompatActivity
{
    class Number
    {
        int num;
    }
    public void clickFunction(View view)
    {
        Log.i("Info","Button Pressed");
        EditText e1=(EditText)findViewById(editText);
        Number myNumber= new Number();
        String NumberEnteredStr = e1.getText().toString();
        Log.i("Number Entered",NumberEnteredStr);
        myNumber.num = Integer.parseInt(NumberEnteredStr);//line 22
    }

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

1 Answer 1

1

As you can see in the stacktrace:

For input string: ""

the value of the input field is an empty string

An empty String is not a number. So you have to check if the field is not empty:

myNumber.num = NumberEnteredStr.isEmpty() ? 0 :Integer.parseInt(NumberEnteredStr);//line 22

BTW: Take care of java naming conventions. variable names should start with lower case character.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.