0

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['[email protected]','[email protected]','[email protected]'] and so and and so forth to [email protected]

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.

0

4 Answers 4

4

Try instantiating your array with

String[] sEmails = new String[10];

You can also make that loop far more succinct:

public void populateEmailArray()
{
    for (int x = 0; x < 10; x++)
    {

        sEmails[x] = sRepStuff + x + sGmail; 
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I originally only had a for loop without the switch case but I got the error and thought that might be causing it, thanks for your help!
If i dont know the max value of array size then how to handle the same?
@Arnab If you want to have a dynamic array, try something like an ArrayList instead - it behaves much like an array but dynamically expands.
Currently ArrayList is deprecated. Use List or Dictionary or such collections...
0

Hope you are doing well.

I will help you clean up your code today!

rather than doing a switch-case you can do this

for(int i = 0; i < 10 ; i++)
{
   emails[i]="repstuff"+i+"@gmail.com";
}

That helps you clear out a your coding style. In addition, did you check if you have instantiated / created both your sEmails, repStuff and Gmail?

Comments

0

Arrays start indexing at 0, not 1, and so you aren't giving sEmails[0] a value. Shift all your values down by 1. Then when you access sEmails[0], it will still be null. You should also make sure that your sEmails array has been instantiated:

sEmails = new String[10];

This should work:

public void populateEmailArray()
{
       sEmails = new String[10];
        for (int x = 0; x < 10; x++)
        {
            switch(x)
            {
                case 0:
                    sEmails[x] = sRepStuff + "1" + sGmail; 
                break;
                case 1:
                    sEmails[x] = sRepStuff + "2" + sGmail;
                break;
                case 2:
                    sEmails[x] = sRepStuff + "3" + sGmail;
                break;
                case 3:
                    sEmails[x] = sRepStuff + "4" + sGmail;
                break;
                case 4:
                    sEmails[x] = sRepStuff + "5" + sGmail;
                break;
                case 5:
                    sEmails[x] = sRepStuff + "6" + sGmail;
                break;
                case 6:
                    sEmails[x] = sRepStuff + "7" + sGmail;
                break;
                case 7:
                    sEmails[x] = sRepStuff + "8" + sGmail;
                break;
                case 8:
                    sEmails[x] = sRepStuff + "9" + sGmail;
                break;
                case 9:
                    sEmails[x] = sRepStuff + "10" + sGmail;
                break;
            }
        }
    }

A better, more concise version would be:

for(int i = 0; i < 10 ; i++)
{
   sEmails[i]="repstuff"+(i+1)+"@gmail.com";
}

4 Comments

But that wouldn't cause a null reference exception, i think it is more likely that the array hasn't been instantiated at all yet.
This is still horrible coding style.
Updated to include that possibility. I was thinking that it threw the exception when it executed something like sEmails[0].length()
@Kevin I included a better implementation.
0

To the solution that you already accepted I would add some "spice" to make it even more dynamic. I would not set 10 hard-coded but I would use array's length instead.

public void populateEmailArray()
{
    int length = sEmails.Length;

    for (int x = 0; x < length; x++)
    {

        sEmails[x] = sRepStuff + x + sGmail; 
    }
}

I don't trust my memory when I have to return to a program some time afterwards and have to remember and check all the points that I have to change when for example your emails array must grow up to 20 elements.

1 Comment

You should not store the length of the array in a local variable. The x86 jitter will remove the array bounds check if you compare the loop variable directly with the length property, but it doesn't do this if the value is assigned to a local. (At least, that was true of the v 2.0 x86 jitter; it may have changed in the meanwhile.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.