0

The project is an assignment on an ecommerce application and I'd be delightful with an assistance. I'm trying to check if product quantity is greater than the product in the cart in the Firestore console but I keep having the NumberFormatException error. Below is the code for when the user tries to increase the number of order which is giving the error

 holder.itemView.findViewById<ImageButton>(R.id.ib_add_cart_item).setOnClickListener {
    // converting the carQuantity to Int from String
    val cartQuantity: Int = cartProductItemListModel.cart_quantity.toInt()

          
    // check if the cart_quantity is less than the stock_quantity
    if (cartQuantity < cartProductItemListModel.product_quantity.toInt()){
            val itemHashMap = HashMap<String, Any>()
            itemHashMap[Constants.CART_QUANTITY] = (cartQuantity + 1).toString()
            if (context is CartListActivity){
                context.showProgressDialogue("Updating Cart")
            }
            FirestoreClass().updateMyCartItem(context, cartProductItemListModel.id, itemHashMap)
    } else{
        if (context is CartListActivity){
            Toast.makeText(context, "Available stock for your order is (${cartProductItemListModel.product_quantity}). " +
                    "You can not add more than stock quantity", Toast.LENGTH_LONG).show()
        }
    }
}

The error from the debugging console is this

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hardextech.store, PID: 9807
java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:627)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter.onBindViewHolder$lambda-4(CartItemListAdapter.kt:126)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter.$r8$lambda$cL9k5ufbU_up2kCEhVgh9sgpi9I(Unknown Source:0)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter$$ExternalSyntheticLambda3.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7044)
    at android.view.View.performClickInternal(View.java:7017)
    at android.view.View.access$3200(View.java:784)
    at android.view.View$PerformClick.run(View.java:26596)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6819)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

The code for reducing the number of order works perfectly which is similar to when the user increase the order.

  holder.itemView.findViewById<ImageButton>(R.id.ib_remove_cart_item).setOnClickListener {
    // when the user clicks on the remove imageButton
    if (cartProductItemListModel.cart_quantity == "1"){
        // perform the same end function as when the user clicks on the delete button
        FirestoreClass().deleteItemFromCart(context, cartProductItemListModel.id)
       } else{
           // converting the cart_quantity from string to Int
           val cartQuantity: Int = cartProductItemListModel.cart_quantity.toInt()
        // creating an HashMap for updating the changes
           val itemHashMap = HashMap<String, Any>()
           itemHashMap[Constants.CART_QUANTITY] = (cartQuantity - 1).toString()

           //show the progress Dialogue
           if (context is CartListActivity){
               context.showProgressDialogue("Updating Cart")
           }
           FirestoreClass().updateMyCartItem(context, cartProductItemListModel.id, itemHashMap)
       }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Adewale
  • 23
  • 7
  • What is unclear about the error message? You cannot convert an empty string to an integer. You could always try `val myInt = myString.toIntOrNull() ?: 0` instead of `toInt()` to have it fall back to 0 if the string isn't a valid integer... – Tyler V Jan 07 '22 at 17:39
  • Thanks. I did that but it's I keep having the toast message from from the else statement – Adewale Jan 07 '22 at 18:07
  • So print out the string values of cart quantity and product quantity before the if statement to see what's going on. – Tyler V Jan 07 '22 at 18:08
  • And you should really read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java before diving into Android programming... – Gyro Gearless Jan 10 '22 at 08:30

1 Answers1

1

If you try to parse "" Into int it will through same exception, before calling to toInt make sure string is not blank or null

  • Thanks... It works if I assign value to it but I can only increase the order to the number assigned to it and not to the actual number product_quantity available – Adewale Jan 07 '22 at 18:27