0

I get the following error in the browser View in jTable after the code has complied and ran: Object reference not set to an instance of an object. I debugged the code and at this line of the code

newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();

I get the mentioned error. I also confirmed it by commenting out this line of the code.

I am trying to do the sorting by hitting the table's column headers, but it's not doing it and gives the error out. Been on it for good few weeks trying to figure out but it just doesn't seem to work. Any help would be a great help and thanks in advance.

Here's the full code for your inspection:

public JsonResult TopPlayedInVenueList1(string StartDate = "", string EndDate = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
    {
        try
        {

            if (Request.IsAuthenticated == true)
            {
                string Path = @"C:\\5Newwithdate-1k.xls";
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                con.Close();
                System.Data.DataTable data = new System.Data.DataTable();
                da.Fill(data);

                List<TopPlayed> daa = new List<TopPlayed>();

                foreach (DataRow p in data.Rows)
                {
                    TopPlayed top = new TopPlayed()
                    {
                        TrackID = Convert.ToInt32(p.Field<double>("TrackID")),
                        Date = p.Field<DateTime>("DateTimes"),
                        TrackName = p.Field<string>("TrackName"),
                        ArtistName = p.Field<string>("ArtistName"),
                        Times = Convert.ToInt32(p.Field<double>("Times"))
                    };

                    daa.Add(top);
                }

                var listOrder = daa.Where(i => i.Date >= Convert.ToDateTime(StartDate) && i.Date <= Convert.ToDateTime(EndDate)).ToList();

                if (jtStartIndex + 150 > listOrder.ToList().Count)
                {
                    int val = listOrder.ToList().Count - jtStartIndex;
                    jtPageSize = val;
                }

                var newlist = listOrder.OrderByDescending(i => i.Date).ToList().GetRange(jtStartIndex, jtPageSize);

                if (string.IsNullOrEmpty(jtSorting)) { jtSorting = "Date ASC"; }

                SortDirection sortDirection = jtSorting.ToLower().Contains("desc") ? SortDirection.DESC : SortDirection.ASC;
                string sortExpression = sortDirection == SortDirection.DESC ? jtSorting.ToLower().Replace(" desc", "") : 
                    jtSorting.ToLower().Contains(" asc") ? jtSorting.ToLower().Replace(" asc", "") : jtSorting;

                if (sortDirection == SortDirection.ASC)
                {
                 newlist = newlist.OrderBy(item => GetPropertyValue(item, sortExpression)).ToList();
                }
                else
                {
                 newlist = newlist.OrderByDescending(item => GetPropertyValue(item, sortExpression)).ToList();
                }

                return Json(new { Result = "OK", Records = newlist, TotalRecordCount = listOrder.ToList().Count });
            }

Edit: GetPropertyValue method:

public static object GetPropertyValue(object obj, string propertyName)
    {
        return obj == null ? null : obj.GetType().GetProperty(propertyName).GetValue(obj, null);
    }
pv619
  • 409
  • 11
  • 29

1 Answers1

0

Make sure your newList contains elements and sortExpression has appropriate value.

Andrew
  • 3,648
  • 1
  • 15
  • 29
  • thanks - I checked that while debugging the code. `newList` contains the data and so does `sortExpression`. Am I getting this error because I am getting data from a local excel file? – pv619 Apr 22 '14 at 07:08
  • Than it's the `GetPropertyValue` method that throws exception. – Andrew Apr 22 '14 at 07:14
  • thanks Andrew - I have updated my code above with the `GetPropertyValue` method for your inspection. You would know better if you could check and don't mind please. – pv619 Apr 22 '14 at 07:17
  • `obj.GetType().GetProperty(propertyName)` means you are trying to get property named with value of `sortExpression` in type `TopPlayed`. That one doesn't exist I believe. (You've used `...jtSorting.ToLower()..` and properties in you type are named with first letter being capital, right?) – Andrew Apr 22 '14 at 07:23
  • thanks for looking into the `GetPropertyValue` method and you're absolutely right. However, you made a good point being `ToLower` so, I just renamed the properties to lowercase and still got the same error. – pv619 Apr 22 '14 at 07:32
  • what's the result of this `obj.GetType().GetProperty(propertyName)` code – Andrew Apr 22 '14 at 07:33
  • thanks for that Andrew - `obj.GetType()` value is the first row of data from the `TopPlayed` list and value for `GetProperty(propertyName)` is `date` coming from `sortExpression` or `if (string.IsNullOrEmpty(jtSorting)) { jtSorting = "date ASC"; }`. hope this is what you had asked for :) – pv619 Apr 22 '14 at 07:46
  • so `obj.GetType().GetProperty(propertyName)` evaluates to `date` `PropertyInfo` which for sure is part of your `TopPlayed` type, and has a getter – Andrew Apr 22 '14 at 08:04
  • yes Andrew, exactly! - going crazy over this past few weeks. Don't really know what really is causing the issue. – pv619 Apr 22 '14 at 08:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51162/discussion-between-andrew-and-pv619) – Andrew Apr 22 '14 at 08:10