Newbie: trouble with the tutorial

2 posts, 1 answers
  1. ColdCold
    ColdCold avatar
    2 posts
    Member since:
    Jul 2007

    Posted 14 Sep 2009 Link to this post

    Hello all,

    I just started working through the first tutorial in the "OpenAccess Made Easy" book and I'm running into a problem already... must be something simple.  I am getting NullReferenceException (Object reference not set to an instance of an object.) at the line "customer.Orders.Add(order);".  Not sure why.  The tables in the DB get created just fine, but then I hit this exception.  It's a simple console application.  Here is my code:

    class Program

    {

        static void Main(string[] args)

        {

     

            IObjectScope scope = ObjectScopeProvider1.ObjectScope();

            scope.Transaction.Begin();

     

     

            Customer customer = new Customer();

            customer.CustomerName =

    "Bob";

     

     

            customer.CustomerNumber = 123;

     


            Order
    order = new Order();

            order.OrderNumber = 1;

            customer.Orders.Add(order);  // <--- exception happens here.

     

            scope.Add(customer);

            scope.Transaction.Commit();

        }

    }

     



    [Telerik.OpenAccess.
    Persistent]

     

    public class Customer

     

    {

        private int customerNumber;

     

        public int CustomerNumber

        {

     

            get { return customerNumber; }

     

            set { customerNumber = value; }

        }

     

     

        private string customerName;

     

        public string CustomerName

        {

     

            get { return customerName; }

     

            set { customerName = value; }

        }

     

     

        private IList<Order> orders; 

     

        public IList<Order> Orders

        {

     

            get { return orders; }

     

            set { orders = value; }

        }

    }

     

     

    [Telerik.OpenAccess.

    Persistent]

     

     

    public class Order

     

    {

        private int orderNumber;

     

        public int OrderNumber

        {

     

            get { return orderNumber; }

     

            set { orderNumber = value; }

        }

    }

     

     

    thanks in advance for your help.

     

     

     

  2. Answer
    PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 14 Sep 2009 Link to this post

    Hi ColdCold,

    You are getting this exception because the collection has not been initialized. To put this into code, you should change this line:
    private IList<Order> orders;  
    to
    private IList<Order> orders = new List<Order>();  
    Please note that this exception is not caused by Telerik OpenAccess ORM.

    Best wishes,
    Petar
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
Back to Top