I have this code snippet in C# console application. It is connecting and working fine in console-app environment but when i move this code to Unity3d environment it is through null reference exception. Maybe Unity need different way to impelment this as unity is not thread safe while the original example is developed using threads.
Console APP Snippet
static void Main(string[] args)
{
string bootstrapServers = "localhost:9092";
string schemaRegistryUrl = "Production163:8081";
string topicName = "player";
string groupName = "avro-generic-example-group";
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (var consumer =
new
ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Console.WriteLine($"Error: {e.Reason}"))
.Build())
{
consumer.Subscribe(topicName);
try
{
while (true)
{
try
{
var consumeResult = consumer.Consume();
Avro.Field f;
consumeResult.Value.Schema.TryGetField("favorite_number", out f);
}
catch (ConsumeException e)
{
Console.WriteLine($"Consume error: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
// commit final offsets and leave the group.
consumer.Close();
}
}
}
But I want to run the same code snippet in Unity3d, I have to make some change as unity is not thread safe so I write this code with changes.
Unity Example
IEnumerator Main()
{
string bootstrapServers = "localhost:9092";
string schemaRegistryUrl = "Production163:8081";
string topicName = "player";
string groupName = "avro-generic-example-group";
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (
var consumer = new ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Debug.Log($"Error: {e.Reason}"))
.Build())
{
consumer.Subscribe(topicName);
while (true)
{
var consumeResult = consumer.Consume(TimeSpan.FromMilliseconds(1000));//TimeSpan.FromMilliseconds(50000)//this line through the exception
Avro.Field f;
consumeResult.Value.Schema.TryGetField("favorite_number", out f);
yield return new WaitForSeconds(1);
}
}
Debug.Log("Main end game.");
}
But the code is executing and throws Null Reference Exception
in this line
var consumeResult = `consumer.Consume(TimeSpan.FromMilliseconds(1000));//TimeSpan.FromMilliseconds(50000)//this line through the exception`
Error
NullReferenceException: Object reference not set to an instance of an object KafkaAvroConsumer+d__1.MoveNext () (at Assets/KafkaUnityIntegration/Scripts/KafkaAvroConsumer.cs:51) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Remember, I am using the error code line (consumer.Consume()
)in console app without any parameter.
var consumeResult = consumer.Consume();
Note: I know that null reference
is one of the common error but i am unable to find that why it is occurring as the same code snippet working fine in console app environment.