Search code examples
c#sqlnullreferenceexceptionstreamreader

StreamReader Null Reference Exception


Im trying to read SQL statement from .SQL files in a resource folder, I have 2 .SQL files right now and it reads one correctly and the other returns a NullRefrenceException

Here is my calling of the sql files:

 string sqlFailRecordNoMatch = EmbeddedResource.GetString("Resources.SQLScripts.RecordNumberFailQuery.sql");

Here is the GetString method:

public static string GetString(System.Reflection.Assembly assembly, string name)
{
    System.IO.StreamReader sr = EmbeddedResource.GetStream(assembly, name);
    string data = sr.ReadToEnd();
    sr.Close();
    return data;
}

Solution

  • The only reason you would get a NullReferenceException on one vs. the other is:

    1. The one that's failing isn't set as an Embedded Resource. You can check that by clicking on the file in the Solution Explorer and hitting F4.
    2. You're using the wrong fully qualified path.

    I suspect it's #1.