0

In my program, I'm getting the error:

An unhandled exception of type 'System.NullReferenceException' occurred in POS    System.exe

Additional information: Object reference not set to an instance of an object.

This happens when I try to add something to TransactionList as shown below. TransactionList is a list of class instances, declared like this:

public static List<Transaction> TransactionList { get; set; }

And this is the Transaction class:

class Transaction
{
    public double TotalEarned { get; set; }
    public double TotalHST { get; set; }
    public double TotalCost { get; set; }
    public string Category { get; set; }
    public int DaysSince2013 { get; set; }
}

Any clue whats wrong here? I can't seem to find why this error is being thrown... Thanks!

for (int i = 0; i < (lines / 5); i++)
        {
            TransactionList.Add(new Transaction //Error happens on this line
            {
                TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
                TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
                TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
                Category = stringArray[(i * 5) + 3],
                DaysSince2013 = Convert.ToInt32(stringArray[(i * 5) + 4])
            });
        }
1
  • 3
    Use the debugger: either TransactionList or stringArray are most likely null. Commented Oct 24, 2013 at 18:20

3 Answers 3

6

Just initialize it before your for loop ?

if (TransactionList == null)
   TransactionList = new List<Transaction>();

for (int i = 0; i < (lines / 5); i++)
        {
            TransactionList.Add(new Transaction //Error happens on this line
            {
                TotalEarned = Convert.ToDouble(stringArray[(i * 5)]),
                TotalCost = Convert.ToDouble(stringArray[(i * 5) + 1]),
                TotalHST = Convert.ToDouble(stringArray[(i * 5) + 2]),
                Category = stringArray[(i * 5) + 3],
                DaysSince2013 = Convert.ToInt32(stringArray[(i * 5) + 4])
            });
        }

Or if you don't like that, since you've declared it as static, you could do:

public static List<Transaction> TransactionList = new List<TransactionList>();
Sign up to request clarification or add additional context in comments.

Comments

0

You have to initilize the list before using it. When you get Object reference not set to an instnce error, it means the object physically doesnot exist

Comments

-2

Proprably TransactionList and or stringArray are both null.

try to do this

public static List TransactionList { get; set; }

if(TransactionList  == null)
   TransactionList  = new List<Transaction>();

3 Comments

That doesn't compile.
You cant initialize auto-properties like this.
If you re-work your answer and format your code correctly, I will remove the down vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.