question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using deserialized instance's delegation throws NullPointerException

See original GitHub issue

The following code throws NullPointerException. Is it expected behavior?

package sample

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JSON

interface IntId {
  var id: Int
}

interface IData {
  var msg: String?
}

@Serializable
data class Data(
  override var msg: String? = null
) : IData


@Serializable
data class IdData(
  override var id: Int,
  var data: Data
) : IntId, IData by data

inline fun <reified T : Any> deserialTest(root: T): T {
  val json = JSON.stringify(root)
  println(json)
  val obj = JSON.parse<T>(json)
  println(obj)
  return obj
}

fun main(args: Array<String>) {
  val src = IdData(3, Data("asdf"))
  val restored = deserialTest(src)
  println(restored.msg)
}
> Task :run FAILED
{"id":3,"data":{"msg":"asdf"}}
IdData(id=3, data=Data(msg=asdf))
Exception in thread "main" java.lang.NullPointerException
	at sample.IdData.getMsg(XSerialization.kt)
	at sample.XSerializationKt.main(XSerialization.kt:38)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
IARIcommented, Aug 18, 2019

Delegation does seem to work fine for me, if the Object is instantiated by default in the constructor:

@Serializable
class MyImpl(val base: BaseClass = BaseClass()) : BaseClass by base {
    ...
}
0reactions
NamekMastercommented, Jan 22, 2021

Delegation does seem to work fine for me, if the Object is instantiated by default in the constructor:

@Serializable
class MyImpl(val base: BaseClass = BaseClass()) : BaseClass by base {
    ...
}

This is an accessible workaround for me, Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson & JSONAnySetter: NullPointer Exception during ...
The problem is that the defaultSerializer instance, you are using inside your HidableSerializer, is a ResolvableSerializer (BeanSerializer), ...
Read more >
delegate:Transient val by lazy fails with NullPointerException ...
Workaround is to remove the @delegate:Transient annotation, which however increases serialized class size.
Read more >
Can Your Kafka Consumers Handle a Poison Pill? - Confluent
Now, when either the key or value delegate fails to deserialize a poison pill, the ErrorHandlingDeserializer returns a null value and adds a ......
Read more >
NullPointerException (Java Platform SE 8 ) - Oracle Help Center
Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of...
Read more >
[Solved] com.fasterxml.jackson.databind.exc ...
Example of InvalidDefinitionException In this given example deserialing ... exist): cannot deserialize from Object value (no delegate- or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found