'I have problems with an array in kotlin

Hello I want to make a query I have two variables

var dataObjectProduct = ArrayMap<String, String>() 
var dataObjectProducts = mutableListOf<String>()

Then in a function I load it as follows

dataObjectProduct.set("idProduct", tvIdProduct?.text.toString())
                dataObjectProduct.set("nameProduct", tvNameProduct?.text.toString())
                dataObjectProduct.set("AmountProduct", txtAmountProduct?.text.toString())
                dataObjectProduct.set("PriceProduct", tvPriceProduct?.text.toString())
                dataObjectProduct.set("TotalProduct", totalLine.toString())
                dataObjectProducts.add(dataObjectProduct.toString())

In another function I occupy to obtain the result I am doing it in the following way:

fun viewListsProducts(v: View){
        val dlProductsSale =
            LayoutInflater.from(this).inflate(R.layout.tr_list_sale_products_layout_as, null)
        tvIdProductSale = dlProductsSale.findViewById(R.id.tvIdSaleProduct)
        tvNameProductSale = dlProductsSale.findViewById(R.id.tvNameSaleProduct)
        tvPriceProductSale = dlProductsSale.findViewById(R.id.tvPriceSaleProduct)
        tvAmountProductSale = dlProductsSale.findViewById(R.id.tvAmountSaleProduct)
        tvTotalProductSale = dlProductsSale.findViewById(R.id.tvTotalSaleProduct)
        for (table in dataObjectProducts){
           var product = JSONObject(table)

            tvIdProductSale.text = "1"
            tvNameProductSale.text = product.getJSONObject("idProduct").toString()
        }

        val dlBuilder = AlertDialog.Builder(this).setView(dlProductsSale)
            .setTitle("Productos a Facturar")
            .setNegativeButton("Cancelar", { dialogInterface: DialogInterface, i: Int -> })
            .setCancelable(false)
        dlBuilder.show()

    }

but it's giving me an error I don't know how to proceed can you give me a hand?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ******************, PID: 22161
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
        at android.view.View.performClick(View.java:7862)
        at android.widget.TextView.performClick(TextView.java:15004)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1217)
        at android.view.View.performClickInternal(View.java:7831)
        at android.view.View.access$3600(View.java:879)
        at android.view.View$PerformClick.run(View.java:29359)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
        at android.view.View.performClick(View.java:7862) 
        at android.widget.TextView.performClick(TextView.java:15004) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1217) 
        at android.view.View.performClickInternal(View.java:7831) 
        at android.view.View.access$3600(View.java:879) 
        at android.view.View$PerformClick.run(View.java:29359) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8167) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 
     Caused by: org.json.JSONException: Unterminated object at character 58 of {PriceProduct=1000, idProduct=8029, nameProduct=PRODUCTO GENERICO, TotalProduct=2000.0, AmountProduct=2}
        at org.json.JSONTokener.syntaxError(JSONTokener.java:460)
        at org.json.JSONTokener.readObject(JSONTokener.java:403)
        at org.json.JSONTokener.nextValue(JSONTokener.java:104)
        at org.json.JSONObject.<init>(JSONObject.java:164)
        at org.json.JSONObject.<init>(JSONObject.java:181)
        at ***********************************.SelectProductsInvoice_as.viewListsProducts(SelectProductsInvoice_as.kt:146)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
        at android.view.View.performClick(View.java:7862) 
        at android.widget.TextView.performClick(TextView.java:15004) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1217) 
        at android.view.View.performClickInternal(View.java:7831) 
        at android.view.View.access$3600(View.java:879) 
        at android.view.View$PerformClick.run(View.java:29359) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8167) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 

XD you're right sorry here is the error that I get



Solution 1:[1]

See your stacktrace? It's divided into sections, there's a header for each one that summarises it. If you just read those, it tells you what the error is, then the thing that caused that error, then the thing which caused that, etc:

java.lang.IllegalStateException: Could not execute method for android:onClick
Caused by: java.lang.reflect.InvocationTargetException
Caused by: org.json.JSONException: Unterminated object at character 58 of {PriceProduct=1000, idProduct=8029, nameProduct=PRODUCTO GENERICO, TotalProduct=2000.0, AmountProduct=2}

So your onClick method failed, because there was some reflection error, and that happened because there was a JSONException (i.e. some big problem relating to JSON)

You can look into the calls in that section to see exactly where the problem appears - but you don't need to, that JSONException line explains what the problem is - you've got an "unterminated object" at character 58 of that data it printed. So it thinks you didn't close the object properly - without bothering to count to character 58, I'm gonna assume it's probably because of this:

nameProduct=PRODUCTO GENERICO

That should be in quotes, right? If it hits that space, it thinks it's the end of the object, that's why it's complaining. So it's a problem with your JSON

Hope that helps! Once you learn how to read stacktraces they can give you a lot of help tracking problems down - they can even tell you exactly what the issue is sometimes, so always check if there's a useful message, or even a warning in the logs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cactustictacs