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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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
lankapalaAuthor Commented:
I  try using this but same issue
OrderShipTo[] oShipto = new OrderShipTo[11];

Open in new window

0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
Flabio GatesCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.