2014-03-31 44 views
0

首先,我想告訴我,我是新的.NET開發(MVC 4) ,我試圖跟隨此tutorial實體框架5,但我繼續收到此錯誤「在jqGridMVC2.dll中發生類型爲'System.NullReferenceException'的異常,但未在用戶代碼中處理 附加信息:未將對象引用設置爲對象實例 ,即使我試圖cheeck空值之前。創建一個動態的jqGrid,ASP.net MVC錯誤4

if(propInfo.GetValue(this, null) != null) { 
    } 



private static object GetPropertyValue(object obj, string property) 
    { 
     System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property); 

    // if (obj != null) 
    //  if (propertyInfo.GetValue(obj, null) != null) 
      return propertyInfo.GetValue(obj, null); 

    // return 0; 
    } 



at jqGridMVC2.Extensions.GridExtension.GetPropertyValue(Object obj, String property) in c:\Users\aal\Documents\Visual Studio 2013\Projects\jqGridMVC2\jqGridMVC2\Extensions\Grid\GridExtension.cs:line 70 
    at jqGridMVC2.Extensions.GridExtension.<>c__DisplayClass4`1.<AsJqGridResult>b__2(T item) in c:\Users\aal\Documents\Visual Studio 2013\Projects\jqGridMVC2\jqGridMVC2\Extensions\Grid\GridExtension.cs:line 38 
    at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) 
    at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) 
    at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() 
    at System.Linq.Enumerable.<SkipIterator>d__4d`1.MoveNext() 
    at System.Linq.Enumerable.<TakeIterator>d__3a`1.MoveNext() 
    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source) 
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) 
    at jqGridMVC2.Extensions.GridExtension.AsJqGridResult[T](IQueryable`1 source, String column, String sortOrder, Int32 page, Int32 pageSize) in c:\Users\aal\Documents\Visual Studio 2013\Projects\jqGridMVC2\jqGridMVC2\Extensions\Grid\GridExtension.cs:line 41 
    at jqGridMVC2.Controllers.HomeController.DynamicGridData(String sidx, String sord, Int32 page, Int32 rows) in c:\Users\aal\Documents\Visual Studio 2013\Projects\jqGridMVC2\jqGridMVC2\Controllers\HomeController.cs:line 30 
    at lambda_method(Closure , ControllerBase , Object[]) 
    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) 
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() 

請幫我我n解決此問題 謝謝您提前。

回答

0

我只是這個

private static object GetPropertyValue(object obj, string property) 
{ 
    foreach (var property1 in obj.GetType().GetProperties()) { 
     return property1.GetValue(obj, null); //throws exception TargetParameterCountException for String type 
    } 
    return 0; 
} 
解決錯誤