Question

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.*

Was it helpful?

Solution

There is a null-safe way to do this: instead of

VersionInfo.Trim().Equals(String.Empty)

write

string.IsNullOrWhiteSpace(VersionInfo)

It will not crash if VersionInfo is null, and it will return true if trimming VersionInfo results in an empty string.

OTHER TIPS

You should use String.IsNullOrEmpty method here. See MSDN

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

Well, since I was getting a ridiculous number of down votes on my other question, here it is again in a more blunt and less lighthearted fashion:

  • null represent the lack of an object; and
  • all strings are objects; thus
  • there is no string, of any length, which is null; thus
  • an empty string is never null.

That's it, nothing more. Examine the stack-trace and/or attach a debugger to find out where the null (which is not an empty string) is coming from.

The exception is the result of using expr.somePropertyFieldOrMethod where expr evaluates to null1 hence the Null Reference Exception.

It is the job of you, the developer, to find out which expr was null instead of waiting to see what others suggest might be wrong. As such, I am closing this as "too localized" after answering the question in the title, which is the only question present.

(As in my previous answer, I note that textBoxVersionInfo being null could cause this exception, even when VersionInfo == "" is true. The other alternative is, of course, that that VersionInfo does not represent the empty string.)


1 Technically this exception could be raised arbitrarily, perhaps justified in an Extension Method. However, raising this exception wantonly is not common or good practice or found in the .NET framework and is thus generally a dismissible cause when debugging.

If VersionInfo is NULL is true then VersionInfo.Trim() will give an error.

please use String.IsNullOrEmpty.

If VersionInfo is null, then you can check it with

if(VersionInfo == null)

or

String.IsNullOrEmpty(VersionInfo)

After your response to my comment, you know that VersionInfo is null. The call to Trim() is failing because that will execute before the check if it equals String.Empty.

You should use (string.IsNullOrEmpty(VersionInfo) || VersionInfo.Trim().Length < 1) instead (or string.IsNullOrWhiteSpace(VersionInfo) if you're in .NET 4).

EDIT:

After seeing your response to another answer that you removed Trim() and it still doesn't work...at that point, it's the Equals call that will break. Try the code mentioned above and it should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top