A Exception When Check Unique with a "BelongsTo-FieldType"

Topics: Developer Forum
Sep 2, 2008 at 9:09 AM
Edited Sep 2, 2008 at 9:51 AM
 private void validateUnique(object obj, Type t, ObjectInfo oi, bool IsNew)
        {
                     if (v != null && v.GetType().IsGenericType)
                    {
                        v = v.GetType().GetField("m_Value", ClassHelper.AllFlag).GetValue(v);
                        //here is error when h is a "BelongsTo"
                    }



Object reference not set to an instance of an object.System.NullReferenceException:
Object reference not set to an instance of an object. at Lephone.Data.ValidateHandler.validateUnique(Object obj, Type t, ObjectInfo oi, Boolean IsNew) in Lephone.Data\ValidateHandler.cs:line 122 at Lephone.Data.ValidateHandler.ValidateObject(Object obj) in Lephone.Data\ValidateHandler.cs:line 78 at Lephone.Data.Definition.DbObjectSmartUpdate.Validate() in Lephone.Data\Definition\DbObjectSmartUpdate.cs:line 82


Here is the Error "h"

        [Index(UNIQUE = true, IndexName = "QueryId_SiteId", ASC = false)]
        [Index(UNIQUE = true, IndexName = "Name_SiteId", ASC = false)]
        [BelongsTo, DbColumn("SiteId"), Index]
        public abstract ClassSite Site { get; set; }


------------------
f (v != null && v.GetType().IsGenericType)
                    {
                        Type v1= v.GetType();
                        System.Reflection.BindingFlags v3= ClassHelper.AllFlag;
                        System.Reflection.FieldInfo v2= v1.GetField("m_Value",v3);
                        v = v2.GetValue(v);//here is throw error
                    }

-----------
your 38421 is wonderful!!great!
Sep 2, 2008 at 9:49 AM
 System.Reflection.FieldInfo v2= v1.GetField("m_Value",v3);
v2 is null
Sep 2, 2008 at 9:55 AM
  System.Reflection.FieldInfo[] v2 = v1.GetFields();

the count is 0.
Sep 3, 2008 at 8:13 AM
琢磨了一天这个BUG, 真的不知问题出在哪
Sep 3, 2008 at 8:25 AM
Edited Sep 3, 2008 at 8:27 AM
终于知道了,这是一个由来已久的BUG,只不过原来因为“虚类建立多个唯一索引的bug”导致这句话一直执行不到。$0$0$0$0同样的代码,用原来的版本,也是能够凑巧执行到这句话,执行的时候依然抛出同样的错误。说明不是新bug.
 object v = h.GetValue(obj);
          if (v != null && v.GetType().IsGenericType)
              {
               v = v.GetType().GetField("m_Value", ClassHelper.AllFlag).GetValue(v);
            }
Sep 3, 2008 at 8:43 AM

问题重现:

首先定义

 [Serializable]
    public abstract class TableA : LinqObjectModel<TableA>
    {
        public abstract string Name { get; set; }
    }

    [Serializable]
    public abstract class TableB : LinqObjectModel<TableB>
    {
        [Index(UNIQUE = true, IndexName = "Url_TableAId", ASC = false)]
        public abstract string Url { get; set; }

        [Index(UNIQUE = true, IndexName = "Url_TableAId", ASC = false)]
        [BelongsTo, DbColumn("TableAId")]
        public abstract TableA TB { get; set; }
    }

然后执行如下:

                DbContext de = DbEntry.Context;
                de.DropAndCreate(typeof(TableA));
                de.DropAndCreate(typeof(TableB));

                var t1 = TableA.New();
                t1.Name = "TestName1";
                t1.Save();

                var t2 = TableA.FindById(1);
                var t3 = TableB.New();
                t3.Url = "TestUrl1";
                t3.TB = t2;
                t3.Validate();
//这里抛出异常
                t3.Save();

Coordinator
Sep 3, 2008 at 12:20 PM
done