Home > database >  How to fix Failed to invoke private with no args
How to fix Failed to invoke private with no args

Time:05-18

I can not understand why this error appeared and how to fix it. help me please

Failed to invoke private ru.osport.osportnfc.model.api.PrintCmd() with no args

I looked at the structure of the transmitted data in retrofit2 but did not see any errors

@Keep
interface PrintApi {

    @GET
    suspend fun getPrintCommands(@Url url: String, @Query("bib") bib: Int?, @Query("card") card: String): PrintResponse
   //  fun getPrintCommands(@Url url: String, @Query("bib") bib: Int?, @Query("card") card: String): Single<PrintResponse>
}


@Keep
@Serializable
data class PrintResponse(
    val ok: Boolean? = false,
    val error: String = readLine() ?: "", //test
    val printout: List<PrintCmd>? = null
)

@Keep
@Serializable
sealed class PrintCmd()

@Keep
@Serializable
@SerialName("formatted_text")
data class PrintFormatterTextCmd(
    val content: String,
    val left_margin: Float,
    val letter_width: Float,
    val line_height: Float,
    val font_family: String,
    val font_size: Int,
    val align: String,
    val bold: Boolean,
    val italic: Boolean,
    val underline: Boolean
) : PrintCmd()

then the call itself

@Singleton
class PrintService @Inject constructor(
    private val printManager: PrinterManager,
    @AppScope coroutineScope: CoroutineScope,
    private val api: PrintApi,
    private val eventRepository: EventRepository
) : CoroutineScope by coroutineScope {

    fun print(dao: TagDao, id: Long?, bib: Int?, tagNumber: String = "") = eventRepository.urls.withLatestValue {
        val url = it?.printUrl
        launch {
            try {
                val result = print(url, bib, tagNumber)
                if (id != null) {
                    dao.setPrintStatus(id, if (result) Status.Done else Status.Error)
                }
            }
            catch (e: Exception){
                Log.e("TEST", e.toString())

            }
        }
    }

    suspend fun print(url: String?, bib: Int?, tagNumber: String = ""): Boolean {
        if (url.isNullOrBlank()) {
            return false
        }
        val commands = getCommands(url, bib, tagNumber)
        return commands?.let {
            printManager.printAll(it)
            true
        } ?: false
    }

     private suspend fun getCommands(url: String, bib: Int?, tagNumber: String): List<PrintCmd>? {
    
            try {
                val result = api.getPrintCommands(url, bib, tagNumber)
    
    /**/
                if (result.ok == true && result.error.isBlank()  && result.printout != null ||
                    result.ok == true && result.error == "EVENT_NOT_EDITABLE"  && result.printout != null)
                {
                return result.printout
                }
                else
                {
                    return null
                }
            } catch (e: HttpException) {
                Log.e("TEST", e.message())
                Logger.w(e)
            } catch (e: IOException) {
                Log.e("TEST", e.toString())
                Logger.w(e)
            }
            return null /**/
        } /**/

I tried to change the PrtintCmd structure, but it did not bring me success I do not even understand what this error can be connected with. It appeared after changing the library implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0") to the next library implementation "com.squareup.retrofit2:converter-gson:$retrofit"

and after replacing the code in this section

 .addConverterFactory(Json { ignoreUnknownKeys = true }.asConverterFactory("application/json".toMediaType()))

to the following code in this section

.addConverterFactory(GsonConverterFactory.create())

please help me figure it out

Stack trace:

W/System.err: java.lang.RuntimeException: Failed to invoke private ru.osport.osportnfc.model.api.PrintCmd() with no args W/System.err:
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212) W/System.err: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40) W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243) W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153) W/System.err: at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err: at java.lang.Thread.run(Thread.java:764) W/System.err: Caused by: java.lang.InstantiationException: Can't instantiate abstract class ru.osport.osportnfc.model.api.PrintCmd W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method) W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343) W/System.err: at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110) W/System.err: ... 14 more

CodePudding user response:

Hej Ethernets,

I think the error is that you are trying to instantiate a sealed class:

java.lang.InstantiationException: Can't instantiate abstract class ru.osport.osportnfc.model.api.PrintCmd W/System.err: at 

In your PrintResponse, you are expecting a List of PrintCmd so the converter tries to parse the response to this class, but a sealed class cannot be instantiated. I think you have to change the type to the implementation:

data class PrintResponse(
    val ok: Boolean? = false,
    val error: String = readLine() ?: "", //test
    val printout: List<PrintFormatterTextCmd>? = null
)
  • Related