I have the following code that I cannot figure out how to get to work:

namespace Project2
{
    public static class Variables
    {
        public static List<string[]> tran1;
        public static List<string[]> tran2;
    }
}


    private void Submit_Click(object sender, EventArgs e)
    {
        //Find the schedules
      //  Variables var;

        string[] transaction;
        transaction = new string[3];
        string tran_num;
        string read;

        if (Tran1.Checked)
        {
            tran_num = "T1 ";
        }
        else
        {
            tran_num = "T2 ";
        }

        transaction[0] = tran_num;

        if (Read.Checked)
        {
            read = "Read";
        }
        else
        {
            read = "Write";
        }

        transaction[1] = read;
        transaction[2] = DATA.Text;

        if (transaction[0] == "T1")
        {
            Variables.tran1.Add(transaction);
            Transaction1.Text += "\n" + read + DATA.Text;
        }
        else
        {
            Variables.tran2.Add(transaction);
            Transaction2.Text += "\n" + read + DATA.Text;
        }

    }

The following is the error that Visual Studio returns when I hit the Add Transaction step button

Untitled62

I am not sure what this error means, and being new to this language, not sure how to fix it.

Thank you for any possible help, or examples of how to fix this.

Please note that the above is actually code snippets from two pieces of code, one only having the class that is at the top, and the other is just a small piece of my main C# program. All are in the same namespace.

So, what you're really asking is if you can add a string array to a list of string arrays?
Did you call new on your "Variables" instance?

Variables myVars = new Variables(); //

Can you step through this in the debugger and tell the exact line where the exception is thrown?

Also, you might want to choose different names for your class and don't use the keyword "var" as a variable/class-instance name.

You didnt tell us a thing concering your issue. You only pasted some code and some error.
Please, tell us more about it.
Where this error occurs, and so on?

You have plenty of variables inside this code, that we dont even know what they really are (ie: transaction, Variables,...)
Please more info needed if you want to get some decent help from us.

I'm guessing it occurs on line 46 or 51 depending on how you set the checkboxes.

What the error means is that you've declared a spot to hold an object, but never created the object. You do this in lines 5-6 of your code. tran1 and tran2 are spots to hold objects, but you never actually create objects for them to hold. Change the code to

public static class Variables {
    public static List<string[]> tran1 = new List<string[]>();
    public static List<string[]> tran2 = new List<string[]>();
}

As a side note, generally the use of global variables means your solution isn't very object oriented.

Thank you all for your help, I did figure out the solution lastnight, it was a combination of the suggestion by Momerath, and the need to build a constructor for my class. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.