C# Class issue

Hi I'm getting Error Message
System.NullReferenceException: Object reference not set to an instance of an object.
See my code
  public partial class Order  {
public OrderShipTo[] ShipTo {
            get {
                return this.shipToField;
            }
            set {
                this.shipToField = value;
                this.RaisePropertyChanged("ShipTo");
            }
        }

}

public partial class OrderShipTo : OrderMailer 
{





}

public partial class OrderMailer 
{

 [System.Xml.Serialization.XmlElementAttribute(Order=5)]
        public string CompanyName {
            get {
                return this.companyNameField;
            }
            set {
                this.companyNameField = value;
                this.RaisePropertyChanged("CompanyName");
            }
        }


}



Calling Class:
Order newOrders = new Order();
OrderShipTo oShipto = new OrderShipTo();
            OrderMailer m1 = new OrderMailer();
            newOrders.ShipTo[1] = oShipto;
            
            newOrders.ShipTo[1].CompanyName = "Testing1";

Open in new window


Error Message
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 94:             OMS.OrderShipTo oShipto = new OMS.OrderShipTo();
Line 95:             OMS.OrderMailer m1 = new OMS.OrderMailer();
Line 96:             newOrders.ShipTo[1] = oShipto;  <------------------------------------Error (NullReferenceException: Object reference not set to an instance of an object.)
LVL 2
lankapalaAsked:
Who is Participating?
 
Flabio GatesConnect With a Mentor Commented:
something like:
newOrders.ShipTo = new OrderShipTo[4]; // initialize ShipTo array with a capacity of 4 items
newOrders.ShipTo[0] = oShipto;         // assign value to first item of your array

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
your ShipTo array from the order class is not initialized
0
 
Flabio GatesCommented:
Just a guess but I think newOrders.ShipTo is null at the point of error and not oShipto.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
lankapalaAuthor Commented:
I  try using this but same issue
OrderShipTo[] oShipto = new OrderShipTo[11];

Open in new window

0
 
lankapalaAuthor Commented:
Hi, Tried using this,
Any idea much appreciated
OrderShipTo oShipto = new OrderShipTo();
        OrderMailer m1 = new OrderMailer();

            newOrders.ShipTo[18] = oShipto;   <---- (NullReferenceException: Object reference not set to an instance of an object)

Any example great help.
Thanks
0
 
Éric MoreauSenior .Net ConsultantCommented:
ShipTo contains nothing so you cannot access index 18.
0
 
lankapalaAuthor Commented:
@Éric Moreau: Can you give a example how to resolve this.
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
Arrays are zero based.  That means the first member of an array has the index 0
so
OrderShipTo[] oShipto = new OrderShipTo[11];
means that oShipto[0] up to and including oShipto[10] are the valid array members.  trying to use oShipto[11] will fail with an error.  (See the previous comment by Flabio Gates).
0
 
lankapalaAuthor Commented:
How to find array size , because it's look like for me Dynmic
0
 
Éric MoreauSenior .Net ConsultantCommented:
If you want something dynamic, better use a list and use the Add method to add an item to your structure.
0
 
lankapalaAuthor Commented:
Thanks, any example. Thx
0
 
Éric MoreauSenior .Net ConsultantCommented:
You have plenty of them on https://www.dotnetperls.com/list
0
 
lankapalaAuthor Commented:
Not this, I'm asking according to my code. Thanks
0
 
Éric MoreauSenior .Net ConsultantCommented:
You can query the Length property of your array to get the size
0
 
lankapalaAuthor Commented:
Thanks all your great help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.