Link to home
Start Free TrialLog in
Avatar of lankapala
lankapala

asked on

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.)
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

your ShipTo array from the order class is not initialized
Avatar of Flabio Gates
Flabio Gates

Just a guess but I think newOrders.ShipTo is null at the point of error and not oShipto.
Avatar of lankapala

ASKER

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

Open in new window

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
ShipTo contains nothing so you cannot access index 18.
@Éric Moreau: Can you give a example how to resolve this.
ASKER CERTIFIED SOLUTION
Avatar of Flabio Gates
Flabio Gates

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How to find array size , because it's look like for me Dynmic
If you want something dynamic, better use a list and use the Add method to add an item to your structure.
Thanks, any example. Thx
Not this, I'm asking according to my code. Thanks
You can query the Length property of your array to get the size
Thanks all your great help