I have a string array defined as String[] sEmails; that I am trying to populate with 10 different (but similar) strings (email addresses in this case).
Here is the code I’m trying to use to populate the array.
public void populateEmailArray()
{
for (int x = 0; x < 10; x++)
{
switch(x)
{
case 1:
sEmails[x] = sRepStuff + "1" + sGmail;
break;
case 2:
sEmails[x] = sRepStuff + "2" + sGmail;
break;
case 3:
sEmails[x] = sRepStuff + "3" + sGmail;
break;
case 4:
sEmails[x] = sRepStuff + "4" + sGmail;
break;
case 5:
sEmails[x] = sRepStuff + "5" + sGmail;
break;
case 6:
sEmails[x] = sRepStuff + "6" + sGmail;
break;
case 7:
sEmails[x] = sRepStuff + "7" + sGmail;
break;
case 8:
sEmails[x] = sRepStuff + "8" + sGmail;
break;
case 9:
sEmails[x] = sRepStuff + "9" + sGmail;
break;
}
}
}
The end result I want to be something like this
sEmails['repstuff1@gmail.com','repstuff2@gmail.com','repstuff3@gmail.com'] and so and and so forth to repstuff9@gmail.com
But on the first try of trying to set an sEmails[x] it gives me an error of “NullReferenceException was unhandled. Object reference not set to an instance of an object.”
I have no idea what I’m doing wrong here because the code seems sound in my mind. Any help on this would be greatly appreciated.
Try instantiating your array with
You can also make that loop far more succinct: