[Solved] java.lang.IllegalArgumentException: Topic cannot be null.

Exception:

java.lang.IllegalArgumentException: Topic cannot be null.

Make sure you have your kafka topic created.

Solution:

Add below code to create Kafka topic automatically when not exist,

Here ngdev-topic is the kafka topic name.

Partitions and replicas are additional configurations, add/configure as per your wish.

package com.ngdeveloper.ngdeveloperkafkaproducer.config;

import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;

@Configuration
public class AutoCreateConfig {

	@Bean
	public NewTopic createNgDevTopic() {
		return TopicBuilder.name("ngdev-topic").partitions(3).replicas(3).build();
	}

}

Leave a Reply