lankapala
asked on
C# Class issue
Hi I'm getting Error Message
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
See my code
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.NullReferenceExcept ion: 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.)
System.NullReferenceExcept
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";
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.NullReferenceExcept
Source Error:
Line 94: OMS.OrderShipTo oShipto = new OMS.OrderShipTo();
Line 95: OMS.OrderMailer m1 = new OMS.OrderMailer();
Line 96: newOrders.ShipTo[1] = oShipto; <-------------------------
your ShipTo array from the order class is not initialized
Just a guess but I think newOrders.ShipTo is null at the point of error and not oShipto.
ASKER
I try using this but same issue
OrderShipTo[] oShipto = new OrderShipTo[11];
ASKER
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
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.
ASKER
@Éric Moreau: Can you give a example how to resolve this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
Thanks, any example. Thx
You have plenty of them on https://www.dotnetperls.com/list
ASKER
Not this, I'm asking according to my code. Thanks
You can query the Length property of your array to get the size
ASKER
Thanks all your great help