Exception while executing LINQ query
i am developing this in ASP.NET MVC2
Getting exception:
Object reference not set to an instance of an object
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="Anonymously Hosted DynamicMethods Assembly"
StackTrace:
at lambda_method(ExecutionScope , TrusteeMaster )
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 LexEyeSystem.Controllers.TrusteeMasterController.JsonContractCollection(Int32 page, Int32 rows, String sidx, String sord) in D:\ParallelMinds\Projects\LexEye\Controllers\TrusteeMasterController.cs:line 560
at lambda_method(ExecutionScope , 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.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
I am trying to show jqgrid, for that purpose I have to write this JSON returning action, which contains my this Linq query. Same query running properly on other pages :
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (
from s in sortedList
select new
{
cell = new string[] { 开发者_运维技巧
"TrustContract/Edit/"+ s.TrusteeId.ToString(),
s.FullName,
s.OtherName.ToString(),
s.FatherName,
s.TrusteeGender,
s.ResidentialAddress,
s.CommunicationAddress,
s.Occupation,
s.Nationality,
s.DateOfBirth.Value.ToShortDateString(),
s.PlaceOfBirth,
s.TelephoneNumber,
s.FacsimileNumber,
s.MobileNumbers,
s.EmailId,
s.ElectionIdCardNumber,
s.PANCardNumber,
s.TaxRegistrationNumber,
s.Qualification,
s.ExperienceInYears.ToString(),
s.Resume.ToString(),
"DisclousureInterestDetails/Index/"+ s.TrusteeId.ToString(),//+"?trusteeId="+s.TrusteeId.ToString(),
"ForeignDetail/Create/"+ s.TrusteeId.ToString(),
"InterestedOtherCompanyInformation/Index/"+ s.TrusteeId.ToString(),
"RelativesDetails/Index/"+ s.TrusteeId.ToString()
}
}).ToArray()
};
But why it should not running here?
Edit: LINQ query to get sortedList:
var sortedList = objTrusteeMasterList
.AsQueryable()
.OrderBy(orderBy) // Uses System.Linq.Dynamic library for sorting
.Skip(pageIndex * pageSize)
.Take(pageSize);
Check if the sortedList in you from s in sorteList
is NULL or not.
I think the exception is thrown when LINQ calls MoveNext
method on sortedList
.
I had the same problem. If I call to the ToArray() method after
from s in sortedList
select new ...
I also got an exception. The problem were the calls to ToString(). If the element was null it would fail.
精彩评论