Reputation: 801
I have the below code which reads elements from the UI and puts it in the array. and then for every array element I am trying to check if a string is present in the array, for which I am using an other foreach which iterates over array. This foreach is returing an extra empty string output.
public void fn_AddContact_CodedStep1()
{
string[] ContactContent = new string[30];
int i = 0, j = 0;
int numberOfContacts=0;
bool isthere;
List<Element> Contacts = Pages.CareMechanix.Find.AllByAttributes("class=pull-left contact-description").ToList();
foreach (Element e in Contacts)
{
ContactContent[i] = Convert.ToString(e.InnerText);
file2.WriteLine("Contact Content="+ContactContent[i]);
i++;
}
numberOfContacts = ContactContent.Length;
if (numberOfContacts>0)
{
try
{
foreach (string p in ContactContent)
{
file2.WriteLine("string=" + p);
isthere = p.Contains("Dorek");
if (isthere == true)
{
file2.WriteLine("person found");
break;
}
}
}
catch(Exception e)
{
file2.Close();
}
}
file2.Close();
}
The output in the file2 is something like this.
Contact Content=Evangeline KaliskiblackCircle
Contact Content=Derek TakamineblackCircle
Contact Content=Monica ThaneerblackCircle
Contact Content=qaorg1prov1First aQAorg1prov1LastPCPblackCircle
Contact Content=QAOrg1Mbr1First eQAOrg1Mbr1LastMemberblackCircle
string=Evangeline KaliskiblackCircle
string=Derek TakamineblackCircle
string=Monica ThaneerblackCircle
string=qaorg1prov1First aQAorg1prov1LastPCPblackCircle
string=QAOrg1Mbr1First eQAOrg1Mbr1LastMemberblackCircle
string=
why is the an empty string is returned in the second foreach loop while I have only 5 elements being returned in the first foreach (i.e array) ??
Upvotes: 0
Views: 99
Reputation: 16259
When you declare ContactContent
string[] ContactContent = new string[30];
you are declaring it and initializing it as an array. So it has 30 elements (which are initially all null), and will always have 30 elements. When you iterate over it, you will iterate over all 30, whether they have nulls or you've put a string at that element. For how you seem to be using this, you might want to use a List<string>
. Otherwise, you will need to use a regular for
loop, and use the value in 'i' as part of the terminator condition.
The reason it short circuits before iterating all 30 is because when the first null is hit, a null exception error is thrown and caught.
Upvotes: 1
Reputation: 32266
The issue is that you have an array set to 30 so when you do
foreach(string p in ContactContent)
It's going to iterate 30 times except that on the 6th iteration it will set p
to null
(because the array is initialized with null
values) and cause this line to throw an exception
isthere = p.Contains("Dorek");
Which you catch and then close the file, but before that line you do this.
file2.WriteLine("string=" + p);
Which will write out "string=" because the + operator for string ignores null
.
My suggestion for fixing this is to use an List<string>
instead of an array and simply add your values to it.
List<string> ContactContent = new List<string>();
...
ContactContent.Add(e.InnerText);
Additionally the closing of the file should really be in a finally block or if possible move the code that initializes file2
inside this method and put it in a using block so that it will handle closing it when you finish, or if an exception occurs.
using(var file2 = InitializeFile2Here())
{
// use file2 here
} // file2 will be disposed/closed here.
Upvotes: 2
Reputation: 192
Actually you have 30 elements in the array. The second foreach loop foreach (string p in ContactContent)
enumerates all 30 elements. But for the 6th element you get NPE here: p.Contains("Dorek");
(because p is null) and goes to finally block. But the record for the 6th element is added to the file. That's why you have additional string in the file.
Upvotes: 1