An empty string is a null reference?

B. Clay Shannon-B. Crow Raven picture B. Clay Shannon-B. Crow Raven · Apr 13, 2012 · Viewed 7.2k times · Source

Why would this code (in my form's _Load() event):

FileVersionInfo vi = FileVersionInfo.GetVersionInfo(_fullPath);
String VersionInfo = vi.FileVersion;
if (VersionInfo.Trim().Equals(String.Empty)) {
    VersionInfo = NO_VERSION_INFO_AVAILABLE;
}
textBoxVersionInfo.Text = VersionInfo;

...give me the following err msg when VersionInfo == "" is true?

System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object.*

Answer

ABH picture ABH · Apr 13, 2012

You should use String.IsNullOrEmpty method here. See MSDN

if (String.IsNullOrEmpty(VersionInfo)) {
    VersionInfo = NO_VERSION_INFO_AVAILABLE;}