This is a simple program of string array and string manipulation.
I have a string array called list
that has strings that represent a single line of a file that I read earlier (each line contains DNA chemical sequence like AGCTTTTCATTCT
).
This getStrands()
method is supposed to take out each individual line (which is a single element in list
) that contains a desired substring called sequence
(like "CATTCT") and put all of these lines with the sequence into another string array called result
and return it.
public static string[] getStrands(string[] list, string sequence)
{
List<string> temp = new List<string>();
for (int i = 0; i < list.Length; i++)
{
string x = list[i];
if (x.IndexOf(sequence) != -1)
temp.Add(x);
}
//converts temp List into a string[] to return
string[] result = new string[temp.Count];
for (int i = 0; i < temp.Count; i++)
result[i] = temp.ElementAt(i);
return result;
}
The error: System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object.
The error occurs at this line: if (x.IndexOf(sequence) != -1)
I checked to see if x
or sequence
was null
, but they're not. How come when I try to see if x
contains sequence
, it gives me this error (I also tried the Contains() method and it gave me the same error)?