-3

I'm trying assign value AbstractArAdjustmentLine list property but getting error

Object reference not set

Below are the 2 classes generated through dll, I can't change the class structure.

public class ArAdjustmentCreate : AbstractArAdjustment
{
    public ArAdjustmentCreate(string controlId = null);
    public override void WriteXml(ref IaXmlWriter xml);
}

public abstract class AbstractArAdjustment: AbstractFunction
{
    public List<AbstractArAdjustmentLine> Lines;
    public DateTime? GlPostingDate;
    public DateTime TransactionDate;
    public string CustomerId;
}

public abstract class AbstractArAdjustmentLine : IXmlObject
{
    public string WarehouseId;
    public string EmployeeId;
    protected AbstractArAdjustmentLine();     
}

// Creating instance of ArAdjustmentCreate

ArAdjustmentCreate arAdjustmentCreate = new ArAdjustmentCreate()
            {                
                CustomerId = "23",
                TransactionDate = DateTime.Now,
                GlPostingDate = DateTime.Now,                    
            };    

AbstractArAdjustmentLine arAdjustmentLine = null;                            
arAdjustmentLine.WarehouseId = "788"; // getting error Object reference not set
arAdjustmentLine.EmployeeId = "100";            
arAdjustmentCreate.Lines.Add(arAdjustmentLine);

How to set value in AbstractArAdjustmentLine abstract class?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Letoncse
  • 702
  • 4
  • 15
  • 36
  • 4
    To an instance of a type which inherits from `AbstractArAdjustmentLine`. Either create using `new`, or get from a property/method. – Zev Spitz Mar 15 '21 at 16:57
  • I can't create an instance of AbstractArAdjustmentLine becaue of abstract class. Thanks ! – Letoncse Mar 15 '21 at 16:59
  • I always think that no way to instance abstract class?? – FletcherF1 Mar 15 '21 at 16:59
  • Therefore @Zev says _"something which inherits from AbstractArAdjustmentLine"_. You can't set properties on a `null` anyway. – CodeCaster Mar 15 '21 at 16:59
  • Yes I can't set null and can't create an instance. How can inheritance ? Thanks ! – Letoncse Mar 15 '21 at 17:01
  • See [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Connor Low Mar 15 '21 at 17:06

1 Answers1

3

You set AbstractArAdjustmentLine arAdjustmentLine = null;, probably because you realized you can't instantiate an abstract class, but you need an instance to set the properties. The only practical way you can use an abstract class is by inheriting in a subtype:

abstract class A { }
class B : A { }

// Works:
A a = new B();

// Works:
B b = new B();

// Does not work:
A c = new A();

See null keyword and abstract in the docs, and What is a NullReferenceException, and how do I fix it? from this site.

Connor Low
  • 5,900
  • 3
  • 31
  • 52