-1

In the following code I get the null reference error on the first line in my finally statement - invoice[x].IdNumber = idResult; how can it give me a null reference when I am trying to assign it a value?

class Program
    {
        static void Main(string[] args)
        {
            const int theConst = 999;
            Invoice[] invoice = new Invoice[5];
            int x;
            int idResult = 999; 
            int dayResult = 1;
            double dResult = 0.0;
            string checker;
            for (x = 0; x < invoice.Length; ++x) //for loop to instantiate objects and obtain data
            {
                try
                {
                    Console.Write("Enter invoice number ");
                    checker = Console.ReadLine();
                    if (int.TryParse(checker, out idResult))
                    {
                        idResult = Convert.ToInt32(checker);
                    }
                    else
                    {
                        idResult = theConst;
                    }


                Console.Write("Enter balance ");
                checker = Console.ReadLine();
                if (double.TryParse(checker, out dResult))
                {
                    dResult = Convert.ToDouble(checker);
                }
                else
                {
                    dResult = 0.0;
                }
                Console.Write("Enter due day ");
                checker = Console.ReadLine();
                if (int.TryParse(checker, out dayResult))
                {
                    dayResult = Convert.ToInt32(checker);
                }
                else
                {
                    dayResult = 1;
                }
                invoice[x] = new Invoice(idResult, dResult, dayResult);
            }
            catch (Exception ex)
            {
                Console.WriteLine("enters catch x is " + x);
                idResult = theConst;
                dResult = 0.0;
                dayResult = 1;
                Console.WriteLine(ex.Message + "default value will be used");
            }
            finally
            {
                invoice[x].IdNumber = idResult;
                invoice[x].BalanceDue = dResult;
                invoice[x].DayDue = dayResult;
            }

        }
        Console.WriteLine("Entered Invoices: " + Environment.NewLine);
        for (x = 0; x < invoice.Length; ++x) //first display
        {
            Console.Write("#" + invoice[x].IdNumber + " $" + invoice[x].BalanceDue + " day " + invoice[x].DayDue + "\n");
        }
        Console.Read();
    }
}
class Invoice
{
    private int idNumber;
    public int IdNumber
    {
        get
        {
            return idNumber;
        }
        set
        {
            idNumber = value;

        }
    }
    private double balanceDue;
    public double BalanceDue
    {
        get
        {
            return balanceDue;
        }
        set
        {
            balanceDue = value;
        }
    }
    private int dayDue;
    public int DayDue
    {
        get
        {
            return dayDue;
        }
        set
        {
            dayDue = value;

        }
    }
    public Invoice(int num, double balance, int daydue)
    {
        IdNumber = num;
        BalanceDue = balance;
        DayDue = daydue;
        Console.WriteLine(num + " " + balance + " " + daydue);
        if (daydue < 1 || daydue > 31)
        {

            throw new ArgumentException("Day must be between 1-31 ");
        }
        if (num < 100 || num > 999)
        {
            throw new ArgumentException("ID number must be between 100-999 ");
        }
    }
    public Invoice() : this(999, 0, 1) { }
}

}

Mirodinho
  • 927
  • 11
  • 24

1 Answers1

0

This error will only occur at run-time when an exception is thrown in your try-block, before the object is initialized: i.e. invoice[x] = new Invoice(...); So the error wouldn't appear if the try-block executes without throwing an exception. Keep in mind that the code in "finally" is executed wether an error occurs or not.

What you should do is initialize the 5 objects in your array of Invoice objects.

Include this:

for (int z=0; z < invoice.Length; z++)
                invoice[z] = new Invoice(idResult,dResult,dayResult);

Before entering your for-loop. Otherwise the object is not initialized and therefore it does not have properties.

This could be one of the possible simpler solutions.

Mirodinho
  • 927
  • 11
  • 24