Exception: 'Nullable value is nil'

visual-studio

(Mark Currie) #1

I would think it would be OK if a nullable was nil.

I’m getting this while trying to get the MongoDB driver library working in Silver 10.0.0.2343. It worked fine in old version of Silver.

System.NullReferenceException: ‘Nullable value is nil’
at RemObjects.Elements.System.GenericNullable1.get_Value() in c:\CI\b\elements\937\Source\RemObjects.Elements.Dynamic\Source\GenericNullable.pas:line 11 at RemObjects.Elements.System.GenericNullable1.op_Implicit(GenericNullable`1 aVal) in c:\CI\b\elements\937\Source\RemObjects.Elements.Dynamic\Source\GenericNullable.pas:line 61
at Main.DeviceData.getDeviceOptionalBy__id(String id) in C:\Mark\Chic\Server\MainSwift\Cen\Server\User\DeviceData.swift:line 58"

The exception is thrown somewhere in here. I’ve tried everything I can think of, like replacing Builders < T > with Builders<T!>.

static func findOptional(elementName: String, value: BsonValue) -> T!
{
	var filter = Builders<T>.Filter.Eq(elementName, value)
	var result = mongoCollection.Find(filter)
	if result.Count() > 0
	{
		return result.First()
	}
	return nil
}

As a workaround, I might create C# wrappers for accessing Mongo. I mean, a C# wrapper that is compatible with Silver as I fear the official MongoDB C# API is not. The official MongoDB C# API uses overly clever C#. Whoever wrote that had too much coffee :slight_smile:

Any suggestions?


(Carlo Kok) #2

The exception isn’t here it seems? It’s in getDeviceOptionalBy__id. This function returns null and you seem to be accessing a member on it.


(Mark Currie) #3

You are right…

See anything wrong with my code? BTW, I cannot access HasValue in code, only in the debugger.


(Carlo Kok) #4

temp workaround:
if (ret != null) return ret;
return null;

I’ll log a bug for the issue.


(RemObjects) #5

Thanks, logged as bugs://81471


(Mark Currie) #6

The workaround is working, thanks!


(RemObjects) #7

bugs://81471 got closed with status fixed.


(marc hoffman) #8

That is as designed. hasValue is an implementation detail of the wrapper the.NET runtime uses to implement the nullable value types. in Elements, the nullable is the value, and has the members of the value, nit the wrapper. you can use assigned() or simply != nil to check if the nullable is null or not.