-1

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)?

124356user
  • 11
  • 2

2 Answers2

1

On the code you don't check if list is null (which would throw on the line for (int i = 0; i < list.Length; i++) nor you check for null on each element, that would throw on the line that you said on your question.

Guillermo Mestre
  • 480
  • 3
  • 16
  • Thank you so much! I added an if statement: `if (x == null) continue;` to check for null. It worked. – 124356user Feb 25 '15 at 23:45
  • 1
    If the answer helped please select this to be the most helpful answer! – Quality Catalyst Feb 25 '15 at 23:47
  • One mechanism that I use with functions that take parameter by reference (any `object` accounts for it) is to actually throw `ArgumentNullException`, `ArgumentException`... depending on the case IF the input is NOT expected to not be null. One easy way to check it is to ask yourself "is it valid for the input to be null? how the function should behave if the input is null?", if the answer is "input shouldn't be null" then throw, otherwise code accordingly. Welcome to Stack Overflow, and if the answer helped you please hit the check at the left =) – Guillermo Mestre Feb 26 '15 at 00:25
  • @GuillermoMestre Reference types *aren't* passed by reference by default in C#. References are passed by value in C# unless the `ref` or `out` modifiers are used. – Preston Guillot Feb 26 '15 at 03:09
  • @PrestonGuillot you are right, I wanted to say "any parameter that is a reference type" but said it the other way around. Definitely yesterday was not my day :P – Guillermo Mestre Feb 26 '15 at 09:22
0

As Guillermo said, you should be checking null before accessing those items:

public static string[] getStrands(string[] list, string sequence)
{
    if (list == null) {
        return null;
    }
    List<string> temp = new List<string>();
    for (int i = 0; i < list.Length; i++)
    {
        if (list[i] != null) {
            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;
}
dub stylee
  • 3,012
  • 5
  • 33
  • 57