Calling a "Method?" from two locations help


May 12, 2016
Programming Experience
You can tell from my question I am new at this but here goes:
Here is the Method I call:
        private void DisplayRecord(string[,] myRecords, int currentRecord, int Records)//myRecords start at 0,  ie currentRecord = 0
            textBox1.Text = " ";
            textBox1.Text = myRecords[currentRecord, 1].Trim();

            textBox2.Text = " ";
            textBox2.Text = myRecords[currentRecord, 2].Trim();

            textBox3.Text = " ";
            string DenS = " ";
            myRecords[0, 3] = myRecords[currentRecord, 3].Trim();
            DenS = DencryptString(myRecords[0, 3]);
            textBox3.Text = textBox3.Text + DenS;

            textBox4.Text = Convert.ToString(currentRecord);
            textBox5.Text = Convert.ToString(Records);


I call it using this from my form my Form1.cs:
            string S;//???
            string S1, S2, S3;
            var myRecords = new string[Records, 4];
            // Read the file and display it line by line.  THE RECORD ONLY HAS 3 LINES
            using (StreamReader sr = new StreamReader(path + "\\MyData.txt"))
                for (int i = 0; i < Records; i = i + 1)
                    S = Convert.ToString(i);
                    myRecords[i, 0] = S;
                    S1 = sr.ReadLine();
                    myRecords[i, 1] = S1;
                    S2 = sr.ReadLine();
                    myRecords[i, 2] = S2;
                    S3 = sr.ReadLine();
                    myRecords[i, 3] = S3;

            currentRecord = 5;//FIRST RECORD IS AT 0
            DisplayRecord(myRecords, currentRecord, Records);

It works as I expected, now the call from another Method:
        private void button8_Click(object sender, EventArgs e)
            //Go to first record
            currentRecord = 0;//FIRST RECORD IS AT 0
            DisplayRecord(myRecords, currentRecord, Records);

This is what does not work, I get this:

I don't know how to troubleshoot this.
Last edited by a moderator:
NullReferenceException was unhandled

I forgot to add this to my original post.


  • NullReferenceException Unhandled.JPG
    NullReferenceException Unhandled.JPG
    79.1 KB · Views: 43
A NullReferenceException occurs when you try to access a member of an object that doesn't exist. On this line:
textBox1.Text = myRecords[currentRecord, 1].Trim();
you're trying to get a String from the 'myRecords' array and then call its Trim method. Presumably that element is null, hence the NullReferenceException.

By the way, what's the point of setting the Text of a TextBox to a single space when you then set it to something else immediately afterwards?
Thanks for the tip about Trim()

myRocords array is definitely null when called from button8_Click.

When I call DisplayRecord(myRecords, currentRecord, Records); from anywhere in the Form1_Load
routine all is good, when I call it from the button8_Click it does not work. My myRecords array is null

I guess I need help on how to keep my array from being null

I thought this:
public string[,] myRecords { get; private set; }
public int Records { get; private set; }
public int currentRecord { get; private set; }
that I set at the beginning would make it public for other methods to call

What am I missing?
I think the issue is that you have several variables named "myRecords". It looks like you have one declared at the class level and then several declared locally. If you have a 'myRecords' variable at the class level then you should just refer to that variable all the time and get rid of all the others, including the parameter to the DisplayRecord method.
Top Bottom