简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM