I receive a NullReferenceException at times when utilizing the LookupIn method to retrieve values. The problem seems to be related to the type of value being retrieved (int and bool throw exceptions), but I haven’t found a commonality between which types succeed and fail (Nullable throws exception, but Nullable doesn’t).
The exception is thrown in this method of the API when spec.Bytes is null…
public TContent Content<TContent>(string path)
{
var spec = Value.Single(x => x.Path.Equals(path));
return _sourceTypeSerializerProvider.Serializer.Deserialize<TContent>(spec.Bytes, 0, spec.Bytes.Length);
}
Here is some sample code that generates the exception. Is this an issue with the driver, or am I using the API incorrectly?
using Couchbase;
using Couchbase.Configuration.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ClientConfiguration config = new ClientConfiguration();
config.Servers = new List<Uri>()
{
new Uri("http://localhost:8091")
};
ClusterHelper.Initialize(config);
var bucket = ClusterHelper.GetBucket("default");
var key = "LookupInTest";
bucket.Upsert(key, new TestClass() { StringProperty = "Test String", TestProperty = 1, NullableIntProperty = 2, BoolProperty = true, NullableBoolProperty = false, TestStruct = new TestStruct() { Value = 5 } });
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.StringProperty)
.Execute();
var val = builder.Content<string>("`stringProperty`"); // Succeeds
}
catch { }
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.TestProperty)
.Execute();
var val = builder.Content<int>("`testProperty`"); // Fails
}
catch { }
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.NullableIntProperty)
.Execute();
var val = builder.Content<int?>("`nullableIntProperty`"); // Fails
}
catch { }
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.BoolProperty)
.Execute();
var val = builder.Content<bool>("`boolProperty`"); // Fails
}
catch { }
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.NullableBoolProperty)
.Execute();
var val = builder.Content<bool?>("`nullableBoolProperty`"); // Succeeds
}
catch { }
try
{
var builder = bucket.LookupIn<TestClass>(key)
.Get(x => x.TestStruct)
.Execute();
var val = builder.Content<TestStruct>("`testStruct`"); // Succeeds
}
catch { }
}
}
public class TestClass
{
public string StringProperty { get; set; }
public int TestProperty { get; set; }
public int? NullableIntProperty { get; set; }
public bool BoolProperty { get; set; }
public bool? NullableBoolProperty { get; set; }
public TestStruct TestStruct { get; set; }
}
public struct TestStruct
{
public int Value { get; set; }
}
}