-2

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Throwing an exception on this line:

AnnuityReader.WriteLine(fields[0] + "," + fields[1] + "," + fields[3] + "," + fields[4]);

I know there are some similar posts out here which have been resolved, but I have not figured it out yet. Any input is appreciated.

        InitializeComponent();
    }
    StreamWriter AnnuityReader;
    List<string[]> Accounts = new List<string[]>();
    private int x;

    private void Assignment2_Load(object sender, EventArgs e)
    {
        string currentLine;
        string[] fields = new string[2]; 
        //Create streamreader
        StreamReader AnnuityReader = new StreamReader("annuities.txt");

        while (AnnuityReader.EndOfStream == false)
        {
            currentLine = AnnuityReader.ReadLine(); 
            fields = currentLine.Split(',');
            Accounts.Add(fields);
            cmbAccount.Items.Add(fields[0]);
        }
        AnnuityReader.Close(); //Creates dictionary

    }

    private void cmbAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        lstAccountDetails.Items.Clear();
        int index = cmbAccount.SelectedIndex;
        string[] fields;
        fields = Accounts[index];

        lstAccountDetails.Items.Add(String.Format("{0,10} {1,10}{2,10}", "Rate", "Deposit($)", "Value($)"));
        lstAccountDetails.Items.Add(String.Format("{0,10} {1,10:C}{2,10:C}", fields[1], double.Parse(fields[2]), double.Parse(fields[4])));

    }

    private void btnProcess_Click(object sender, EventArgs e)
    {
        lstAccountDetails.Items.Clear();
        double deposit;
        int index = cmbAccount.SelectedIndex;
        string[] fields = Accounts[index];

        try
        {
            deposit = double.Parse(txtDeposit.Text);
        }
        catch
        {
            MessageBox.Show("Enter a positive number");
            txtDeposit.SelectAll();
            txtDeposit.Focus();
            return;
        }
        double currentValue = double.Parse(fields[4]);
        if (deposit > 0)
        {
            currentValue += deposit;

        }
        else
            MessageBox.Show("Please enter a positive number");

        fields[4] = currentValue.ToString();
        fields = Accounts[x];

        AnnuityReader.WriteLine(fields[0] + "," + fields[1] + "," + fields[3] + "," + fields[4]);
        lstAccountDetails.Items.Add(String.Format("{0,10} {1,10}{2,10}", "Rate", "Deposit($)", "Value($)"));
        lstAccountDetails.Items.Add(String.Format("{0,10} {1,10:C}{2,10:C}", fields[1], double.Parse(fields[2]), double.Parse(fields[4])));

        txtDeposit.Clear();
        AnnuityReader.Close();
    }
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
wavers00
  • 11
  • 1
  • 2
    This answers your question. [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) – Ňɏssa Pøngjǣrdenlarp Nov 21 '19 at 21:13
  • If linked duplicate does not answer your question - please follow all debugging guidance in that question and then [edit] this post with really [MCVE]. – Alexei Levenkov Nov 21 '19 at 21:26
  • That line has seven object references: `AnnuityReader`, `fields`, and `fields[0]` through `fields[4]`. One of those (at least) is null. Put a break point there, and when you reach the breakpoint check the values and find out which is `null`. – Joel Coehoorn Nov 21 '19 at 21:38
  • You need to skip empty lines in the .txt file. if (fields.Length < 5) continue; – Hans Passant Nov 22 '19 at 08:16

1 Answers1

0

Error is pretty clear. Object reference not set to an instance of an object. You are defining StreamWriter AnnuityReader but you are using that reference without setting to an object in btnProcess_Click function.

You can solve it in two ways. You need to set it to an instance in constructor or in your btnProcess_Click function before using. You can set it to an object like you did in Assignment2_Load function.

StreamReader AnnuityReader = new StreamReader("annuities.txt");
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20