Denise
Denise

Reputation: 1088

what can be used as an alternative to Object in Kotlin?

I am writing the following Kotlin code but when I compile it gives the error

Null can not be a value of a non-null type ByteArray this is my code

fun myKafkaProducer(): MessageBusProducer<UUID?, Any>? {
         return if (serverProperties().isWAPKafkaSet) {
            val uuidSerializer = UUIDSerializer()
            val uuidDeserializer = UUIDDeserializer()
            val topic = serverProperties().hmsTopic
            val serializer = KafkaProperties.Serializers(Function<UUID, ByteArray> { uuid: UUID? -> uuidSerializer.serialize(null, uuid) }, label@ Function<Any, ByteArray> { sapMessage: Any ->
                try {
                    return@label ObjectMappers.getObjectMapper().writeValueAsString(sapMessage).toByteArray()
                } catch (e: JsonProcessingException) {
                    e.printStackTrace()
                }
                null /*showing error here */
            }, null,
                    null
            )
            // set WapKafkaHostPortAddress in env (configuration repo)

           return KafkaMessageBusProducerFactory.Builder<UUID, Any>()
                    .kafkaAddressPropertyName("portAddress")
                    .topic(topic)
                    .messageBusTypeSerializers(serializer)
                    .build().create(true)

        } else {
           return null
        }
    }

I am trying to code the equivalent of Serializers<UUID, Object>

what other datatype can I use ?

Upvotes: 0

Views: 532

Answers (1)

Daniel Jacob
Daniel Jacob

Reputation: 1536

The equivalent of Object in Kotlin is the type Any?. Only then are you able to return null, because the type Any is non-nullable and Any? can be null.

Upvotes: 1

Related Questions