Results 1 to 5 of 5

Thread: Bug report: NullReferenceException

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3

    Default Bug report: NullReferenceException

    The following code throws a NullReferenceException:
    public class Company
    {
      public string Name { get; set; }
    }
    
    public class Job
    {
      public Company Company { get; set; }
      public String Title { get; set; }
    }
    
    static void Main(string[] args)
    {
      using (var repository = StorageEngine.FromFile("Test.stsdb"))
      {
        var locator = new Locator("Jobs");
        var jobsTable = repository.Scheme.CreateOrOpenXTable<ulong, Job>(locator);
        jobsTable.ToArray();
      }
    }
    
    If the Name property of the Customer class is removed, there is no exception. Same if it's the Company property of the Job class that is removed.

  2. #2

    Default

    You have to define a default constructor for each class with non-primitive properties and you have to initialize these properties. This is the behaviour that the database expects from your class.

        public class Job
        {
            public Job()
            {
                Company = new Company();
            }
            
            public Company Company { get; set; }
            public String Title { get; set; }
        }
    
    No matter how complicated is one object (object with sub-objects), the engine look at it as a linear list of primitive properties with nested names. In your case the Job class is interpreted as following pseudo-code:

        public class Job
        {
            public string Company.Name { get; set; }
            public string Title { get; set; }
        }
    
    In short, when we add records to the database, the engine will split them as arrays of primitive properties and will store/compress them vertically. That's why for each instance of the storred class, all accesses to their "leaf" properties must be valid, i.e. "middle" properties must be non-null values.

    All non-primitive properties of the record must be initialized in the default constructor before writing of the record to the database.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3

    Default

    I understand. Thank you for the explanation.

    As I understand it, instances in the "Company" table and Company instances in Jobs won't be related. Joins must be used in all cases.
    Do you confirm?

    See the test on my blog.

  4. #4

    Default

    Yes, if you define a separate table "Company" with Company records, there will be no relation between "Company" objects in "Jobs" table and "Company" objects in "Company" table.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    3

    Default

    Thanks for the clarification.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
2002 - 2014 STS Soft SC. All Rights reserved.
STSdb, Waterfall Tree and WTree are registered trademarks of STS Soft SC.