CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    strange NullReferenceException

    Hi guys,

    I get some strange NullReferenceException once in a while (see screenshot). The 'TempDatasource' variable is a Dictionary<int, DataItem>, where DataItem is a custom class. The variable 'rn' is an integer.

    I'm using VS2010 & .Net 4. Could this be a bug in the framework?
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: strange NullReferenceException

    What is the stack? Haven't you defined you own GetHashCode(), Equals() method in DataItem?
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: strange NullReferenceException

    The DataItem class is a simple class that only contains some int and string properties. No methods defined. I created this class because I need to bind some data to a gridview from different sources.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: strange NullReferenceException

    Can you post the code?
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: strange NullReferenceException

    (only the relevant code)
    Code:
      public partial class cpm : System.Web.UI.Page {
        private bool completed = false;
        private Dictionary<int, DataItem> TempDatasource = new Dictionary<int, DataItem>();
    
        class DataItem {
          public int rn_number { get; set; }
          public int imps { get; set; }
          public string name { get; set; }
          public DateTime enddate { get; set; }
        }
    
        protected void Page_Load(object sender, EventArgs e) {
            Report report = new Report();
            report.Completed += Process;
            report.Run(); //runs the report async
    
            while (!completed)
              Thread.Sleep(50);
            
            //now the report is ready, load some data from the local database and set the name and enddata
            //...
    
            gridview1.DataSource = TempDatasource.Values;
            gridview1.DataBind();
        }
    
        void Process(API.ExecuteReportCompletedEventArgs<API.Reporting.Network> e) {
          if (e.error != null) {
            //log error
          }
          else {
            foreach (var item in e.result.Data) {
              int rn;
              if (RN.IsValid(item.line_item, item.line_item_id, out rn)) {
                if (!TempDatasource.ContainsKey(rn)) {
                  TempDatasource.Add(rn, new DataItem());
                  TempDatasource[rn].rn_number = rn;
                }
                TempDatasource[rn].imps += item.imps;
              }
            }
          }
          completed = true;
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured