views:

107

answers:

4

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?

A: 

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

Sander Pham
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.
Nightforce2
A: 

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.

spinon
This is exactly what I am looking for! Rock on! It crashes no more =)
Nightforce2
+2  A: 

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>);
Aren
I've used all three of these methods before, each with equal success. Nicely put, Aren.
Adam McKee
@Adam: Thanks :)
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.
Nightforce2
A: 

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

Steven Sudit