Why is this code (in my _Load()event form ):
_Load()
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 msg error when VersionInfo == ""true?
VersionInfo == ""
Error System.NullReferenceException Message = The object reference is not set to the object instance. *
There is a nullsustainable way to do this: instead
null
VersionInfo.Trim().Equals(String.Empty)
records
string.IsNullOrWhiteSpace(VersionInfo)
It will not work if VersionInfo- null, and it will return true if trimming VersionInfoleads to an empty string.
VersionInfo
Here you should use the method String.IsNullOrEmpty. See MSDN
String.IsNullOrEmpty
if (String.IsNullOrEmpty(VersionInfo)) { VersionInfo = NO_VERSION_INFO_AVAILABLE;}
, , :
, . / , , null ( ).
expr.somePropertyFieldOrMethod, expr null 1 , Null Reference.
expr.somePropertyFieldOrMethod
expr
, , , expr null, , , , , . , " " , .
( , , textBoxVersionInfo, null, , VersionInfo == "" . , , VersionInfo .)
textBoxVersionInfo
1 , , . .NET , , .
VersionInfo is NULL , VersionInfo.Trim() .
VersionInfo is NULL
String.IsNullOrEmpty.
VersionInfo NULL,
if(VersionInfo == null)
String.IsNullOrEmpty(VersionInfo)
After your reply to my comment, you know what VersionInfois NULL. Trim () is not String.Emptycalled because it will be executed before checking if it is equal .
String.Empty
Instead, you should use (string.IsNullOrEmpty(VersionInfo) || VersionInfo.Trim().Length < 1)(or string.IsNullOrWhiteSpace(VersionInfo)if you are in .NET 4).
(string.IsNullOrEmpty(VersionInfo) || VersionInfo.Trim().Length < 1)
EDIT:
Seeing the answer to another answer that you deleted Trim () and it still doesn't work ... at that moment it will break the Equals call. Try the code mentioned above and it should work fine.