Ted
Ted

Reputation: 20184

ServiceStack.OrmLite: NullReferenceException in ServiceStack.Text.AssemblyUtils.ToTypeString(Type type)

As I continue testing out OrmLite, I ran into another problem, this one appears to happen in the ServiceStack.Text.AssemblyUtils:

System.NullReferenceException: Object reference not set to an instance of an object.
   at ServiceStack.Text.AssemblyUtils.ToTypeString(Type type)
   at ServiceStack.Text.Common.JsWriter`1.WriteType(TextWriter writer, Object value)
   at ServiceStack.Text.Common.WriteListsOfElements`2.<>c__DisplayClass1_0.<.cctor>b__0(TextWriter writer, Object obj)
   at ServiceStack.Text.Common.WriteListsOfElements`2.WriteGenericIList(TextWriter writer, IList`1 list)
   at ServiceStack.Text.TypeSerializer.SerializeToString(Object value, Type type)
   at ServiceStack.Text.TypeSerializer.SerializeToString[T](T value)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.GetFieldValue(FieldDefinition fieldDef, Object value)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.GetValueOrDbNull[T](FieldDefinition fieldDef, Object obj)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetParameterValue[T](FieldDefinition fieldDef, IDataParameter p, Object obj)
   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetParameterValues[T](IDbCommand dbCmd, Object obj)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.Insert[T](IDbCommand dbCmd, T obj, Action`1 commandFilter, Boolean selectIdentity)
   at ServiceStack.OrmLite.UntypedApi`1.<>c__DisplayClass16_0.<Insert>b__0(IDbCommand dbCmd)
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
   at tWorks.Core.CoreServerCommons.Handlers.OrmLiteDbHandler.<>c__DisplayClass46_0.<AddCoreObjectToDatabase>b__0(IDbConnection db) in D:\[GIT]\Core\CoreServerCommons\Handlers\DbHandlers\OrmLite\OrmLiteDbHandler.cs:line 253

It happens when I what to insert an object, using the Untyped API, like this:

long insertedId = dbCluster.GetDbAndRun((IDbConnection db) =>
{
    coreObject.Id = id;
    var typedApi = db.CreateTypedApi(coreObject.GetType());
    return typedApi.Insert(coreObject, selectIdentity: true);
});

The 'coreObject' I am trying to insert:

public class ModuleController : Actor
{
    public ModuleController(string username, List<Type> contactTypes)
    {
        this.Username = username;
        this.ContactTypes = contactTypes;

        this._AllowedLoginChannels = new List<Type>();
        this._AllowedLoginChannels.Add(typeof(CoreClient));
    }
    public List<Type> ContactTypes { get; set; }
}

I managed to figure out where the NullRef comes from:

enter image description here

Basically, if there is a NULL entry in the ContactTypes List, then it fails as above.

Now, in this case, the list shouldn't contain a NULL value, but I would feel a lot better if I didn't get a NullRef deep inside ServiceStack because of it, hindering me from persisting the object. Maybe I want a NULL there?

Is it just in List, or a Dictionary can't have NULL values either? What else cannot be NULL when persisting?

Shouldn't ServiceStack just handle it as null, so in its JSON format, that would simple be null?

Upvotes: 1

Views: 276

Answers (1)

mythz
mythz

Reputation: 143284

OrmLite uses the JSV Format to serialize complex type blobs by default, it’s a white space sensitive that uses CSV Format by which doesn’t include a null data type as that would be inferred as a “null” string, instead any null properties are simply excluded from the wire format which has the same effect as retaining the default property value when it’s deserialized.

If you want to include nulls in your serialized data you can change the text serializer used to serialize complex types, e.g:

PostgreSqlDialect.Provider.StringSerializer = new JsonStringSerializer();

Upvotes: 1

Related Questions