This is a migrated thread and some comments may be shown as answers.

Newbie: trouble with the tutorial

1 Answer 57 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ColdCold
Top achievements
Rank 1
ColdCold asked on 14 Sep 2009, 05:49 AM
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.

 

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
PetarP
Telerik team
answered on 14 Sep 2009, 10:29 AM
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.
Tags
Getting Started
Asked by
ColdCold
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or