tags:

views:

94

answers:

2

Hello everybody.

So I have an abstract class named "Account" :

public abstract class Account
{
    private string _FinancialInstitution;

    public string FinancialInstitution
    {
        get { return _FinancialInstitution; }
        ...
    }
}

And I have two other classes that extends from those two:

public class CreditCard : Account
    {
        private DateTime _ExpirationDate;
        ...
    }

and this one:

public class CheckingSavingsAccount : Account
{
    private string _PrimaryAccountHolder;
    ...
}

Now, The whole point was to be able to store either kind of account in a generics collection list, but if I try to do this:

List<Account> lstTemp = new List<Account>();
CreditCard newCC1 = new CreditCard();
lstTemp.Add(new CreditCard());

I got an "Object reference not set to an instance of an object." error on the line that attemps to add the newly credit card object created (lstTemp.Add). What am I doing wrong?


This is the exception detail:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="mscorlib"
  StackTrace:
       at System.Collections.Generic.List`1.Add(T item)
       at RunAsConsole.Program.Main(String[] args) in C:\Users\ortegae\Documents\Visual Studio 2008\Projects\eStocks50600\RunAsConsole\Program.cs:line 52
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
A: 

If you have a constructor for Account() or for CreditCard()

public Account()
{
...
}

public CreditCard()
{
...
}

then it may be that the exception is thrown in there. You probably want to check with the Visual Studio debugger to see what's going on.

A useful tip, BTW, is to set Visual Studio to break on all exceptions, whether handled or unhandled. Under Debug, choose Exceptions..., then next to Common Language Runtime Exceptions make sure the checkbox under Thrown is set on.

Jeremy McGee
Jeremy:The constructors are empty, no code is exceuted there...
Edwin
To the people that have asked for lstTemp.Add(newCC1): either way, with lstTemp.Add(newCC1) or lstTemp.Add(new CreditCard()) gets an error...
Edwin
Have you tried it with the CheckingSavingsAccount type?
Nathan Koop
Ok, I created a new blank console project and it works. I have no idea why this is happening, but on a blank fresh project it works. I'm going to keep investigation. Thank you for all your suggestions.
Edwin
+1  A: 

Hmmm... I don't see anything wrong. I think your problem is not related to your list and must be in some other code you haven't shown. I just ran the following test without failure:

public abstract class Base {}
public class D1 : Base {}
public class D2 : Base {}

[Test]
public void Test_Generic_Lists_With_Abstract_Base()
{
    var list = new List<Base>();
    list.Add(new D1());
    list.Add(new D2());

    Assert.That(list[0] is D1);
    Assert.That(list[1] is D2);
}

EDIT Your stack trace does not line up with the code you showed. The return of new can never be null, and your stack trace shows that null was passed in. What else are we missing?

Brian Genisio