0

Error: java.lang.NumberFormatException

Error in file: activity_main_user.xml

Elements related: one EditText(PlayerMoneyToUpdate) field, and TextView(PlayerMoneyValue)

The structure of the project:

There are two Activities:

MainActivity.java is the main UI of app and contains a layout displaying all the users i.e, players of the game

MainUserActivity.java is the UI which displays the selected player and information related to it such as player name, Money(if exists) and etc.

There are also two layout XML files for this:

activity_main.xml, again, contains the main UI, no problems related to it

activity_main_user.xml, where the error is occurring.

this is the activity_main_user.xml code which has an EditText field:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/UserAvatar"
                android:layout_width="160dp"
                android:layout_height="149dp"
                android:contentDescription="@string/AvatarDescription"
                app:srcCompat="@drawable/monopolycloud" />

            <View
                android:id="@+id/divider8"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="?android:attr/listDivider" />

            <TextView
                android:id="@+id/PlayerId"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="@string/PlayerIdText"
                android:textSize="30sp" />

            <View
                android:id="@+id/divider9"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="?android:attr/listDivider" />

            <TextView
                android:id="@+id/MoneyValue"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:textSize="30sp"
                android:text="@string/DefaultMoney"/>

            <EditText
                android:id="@+id/EditTextField"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:autofillHints="true"
                android:hint="@string/EditTextFieldText"
                android:inputType="number" />

            <Button
                android:id="@+id/AddButton"
                android:layout_width="match_parent"
                android:layout_height="65dp"
                android:text="@string/AddButtonText" />

            <View
                android:id="@+id/divider6"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="?android:attr/listDivider" />

            <Button
                android:id="@+id/SubtractButton"
                android:layout_width="match_parent"
                android:layout_height="65dp"
                android:text="@string/SubtractButtonText" />

            <View
                android:id="@+id/divider7"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:background="?android:attr/listDivider" />

            <Button
                android:id="@+id/GoToMainMenu"
                android:layout_width="match_parent"
                android:layout_height="65dp"
                android:text="@string/GoToMainMenu" />

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

in the activity_main_user.java file, I have defined a function where, upon starting of this file, the playerid and money fields get updated. upon adding a value in the EditText and pressing AddMoney button, it throws an exception java.lang.NumberFormatException for string ""

The main block of code related :

AddButton.setOnClickListener(v -> {
            try {
                mValue1 = Integer.parseInt(NewMoneyValue.getText().toString());
                Log.d("Monopoly Cashier value1", String.valueOf(mValue1));
                mValue2 = Integer.parseInt(MoneyValue.getText().toString());
                Log.d("Monopoly Cashier value2", String.valueOf(Integer.parseInt(MoneyValue.getText().toString())));
                mNewMoney = add(mValue1, mValue2);
                MoneyValue.setText("Current Money: $" + mNewMoney);
                MoneyAddedToast.show();
            } catch (Exception e) {
                Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
                Log.d("Monopoly CashierError: ", e.getMessage());
            }
        });

Please help me I getting really frustrated over this. Thanks in advance.

P.S: I am a beginner to Android programming in java, so please try to be as simple as possible. P.S 2: I know that there are many questions related to my problem but I couldn't understand any of them as they weren't relatable to what I am facing

Esvin Joshua
  • 71
  • 14
  • The exception is thrown because the NewMoneyValue you are getting is null. As null cannot be parsed into an Integer, hence exception is thrown. The problem is not in parsing but in linking your editText with your code. Try with Integer.parseInt("1") and see if it works. If it does, look where you are binding your editext with the code. – Syed Faizan Ali Jun 17 '22 at 14:37
  • I am binding(linking) all the elements inside onCreate() and when i try Integer.parseInt("1500') it works fine. Not able to understand why this is happening... – Esvin Joshua Jun 18 '22 at 17:04
  • Exactly, the problem lies in binding. Can you share your binding code? What approach you are using? view binding, synthetic binding etc? – Syed Faizan Ali Jun 20 '22 at 08:22
  • heres the binding code: Button AddButton = findViewById(R.id.AddButton); Button SubtractButton = findViewById(R.id.SubtractButton); Button GoHomeButton = findViewById(R.id.GoToMainMenu); EditText NewMoneyValue = findViewById(R.id.EditTextField); Toast MoneyAddedToast = Toast.makeText(this, "Money Added!", Toast.LENGTH_SHORT); Toast MoneySubtractedToast = Toast.makeText(this, "Money Removed!", Toast.LENGTH_SHORT); – Esvin Joshua Jun 20 '22 at 09:02
  • The binding code seems fine to me. Maybe share a Github repo of this code? – Syed Faizan Ali Jun 20 '22 at 11:41
  • aah yes hold on... – Esvin Joshua Jun 20 '22 at 16:05
  • https://github.com/devjoshua312/MonopolyCasher I also have one tiny doubt, it's about writing data type *int* to a file... if you just look at MainActivity.java line 42 (which is where the function starts) hoping you can guide me on this... – Esvin Joshua Jun 20 '22 at 16:12
  • Not being able to run this code on my Android studio bruh. What kind of build system you are using for this project? Many of the dependencies are missing. – Syed Faizan Ali Jun 20 '22 at 18:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245778/discussion-between-esvin-joshua-and-syed-faizan-ali). – Esvin Joshua Jun 21 '22 at 05:07
  • The problem lies in this line String digits = MoneyValue.toString().replaceAll("[^0-9]", ""); I don't know what you are trying to do here but the end result of digits is a jargon number that cannot be parsed into an Integer. Remove/Fix that line and everything works fine. – Syed Faizan Ali Jun 21 '22 at 07:33

1 Answers1

0

Please check whether NewMoneyValue or MoneyValue are properly linked with their R.id , because simply one of them isn't getting their value from UI