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)
}
}