0

How do I check for null in a array list and remove them? It keeps giving me a error of "ArgumentOutOfRange was handled" or if I change the SMTPException to just Exception it says "Index out of range". Looking at the debugger and taking a look specifically at mails.Count it gives me Count = 4. I originally gave it 3 emails to check. The last Array is a "". I believe this is causing the problem.

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (textBox1.Text == "")
            {
                textBox3.Text += "[-] Listbox is Empty!!!!\r\n";
                return;
            }
            // textBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); 
            // Grabs Emails supplied in textbox1 amd then seperates array using '\n'
            ArrayList mails = new ArrayList(textBox1.Text.Split('\n')); 

            // Note: For thought 
            // IEnumerable<string> myResults = mails.Split('\n').Where<string>(s => !string.IsNullOrEmpty(s));
            for (int i = 0; i < mails.Count; i++)
            {
                // Seperates user & pass to be passed for authentification.
                ArrayList mailInfo = new ArrayList(mails[i].ToString().Split(':'));
                textBox3.Text += "[+] Checking email format for" + "\r\n" + "[/] " +  mails[i] + "\r\n";
                // Attach domain name if not attached.
                if (!mailInfo[0].ToString().EndsWith("@gmail.com")) mailInfo[0] = mailInfo[0] + "@gmail.com"; 

                // Debug:  Check point
                // 
                // Error message:
                // Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index. 
                // mails.Count = 4 when feeding only 3 emails.
                //
                // Function:  Check mail & Password
                // error: index out of range
                // MessageBox.Show(mailInfo[0].ToString() + "\n\n" + mailInfo[1].ToString());
                if (mails[i] == "") throw new ArgumentOutOfRangeException("No Mail", "No more mail to check.");
                if (checkAccount(mailInfo[0].ToString(), mailInfo[1].ToString()))
                {
                    textBox3.Text += "[+] Connection Successful! Checking mail.: mailInfo[0].ToString()\r\n";
                }
            }
        }

What am I doing wrong??? Is the null my problem and if so how do I remove it or am I missing something?

3
  • 4
    I would politely suggest throwing away ArrayList and replacing it with List<string>, if only to make the code less archaic. Commented Jul 23, 2010 at 20:41
  • 1
    ArrayList was pretty much obsoleted around late 2004. Please consider replacing with the new generic list equivalent List<string> (in your case). This will remove your need for all the .ToString()s all over the place. Commented Jul 23, 2010 at 20:44
  • @Steven, Jesse. Good suggestion Commented Jul 23, 2010 at 21:05

4 Answers 4

2

To remove items from a list by checking conditions can be tricky. If you're not using a nifty LINQ statement (since you're using an ArrayList im going to assume LINQ is not availiable to you) you will need to use reverse iteration.

If you iterate over a list like this:

foreach(object o in ArrayList)
{

}

Or

for(int i = 0; i < myArray.Count; i++)
{
}

You will run into issues removing items because you'll run out of items (since the list is now smaller than when you defined your for/foreach).

You need to use reverse iteration or store a list of items to remove later.

Example 1: Reverse Iteration

for(int i = myArray.Count - 1; i >= 0; i--)
{ 
   object o = myArray[i];
   if (<some condition here>)
       myArray.Remove(i);
}

Example 2: Removal List*

ArrayList toRemove = new ArrayList();
foreach(object o in myArray)
{
   if(<some condition here>)
     toRemove.Add(o);
}

foreach(object o in toRemove)
   myarray.Remove(o);

Example 3: LINQ Approach

var cleanEntities = myArray.Where(x => <some condition on x, which is your item>);
Sign up to request clarification or add additional context in comments.

2 Comments

I've used all three of these methods before, each with equal success. Nicely put, Aren.
I am still pretty new to LINQ, but I like how you approached this pretty quickly. I'll have to try this out sometime.
0

Try populating the array like this:

ArrayList mails = new ArrayList(textBox1.Text.Trim().Split('\n'));

That should remove any trailing white space and remove that last item from getting into the array.

1 Comment

This is exactly what I am looking for! Rock on! It crashes no more =)
0

I think the empty entry is caused by a carriage return after the last item in your textbox.

1 Comment

It can't be a carriage return because its in a string for a textbox seperate from the method being used. The Array on the other hand is not being fed any Carriage returns just newline.
0

It's generally simpler to iterate over one list while building up another. Hard to go wrong that way.

Comments

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.