Error "Main has thrown an exception"


Checking for a null input: If I enter a null for “strLastName” then I get the error shown below. If I enter just spaces then it works correctly. Please let me know what’s going on and how to avoid what looks like a bug to me…



What’s happening is that typing nothing into the dialog makes the string actually null (i.e. Nothing in VB), also if it previously was an empty string. An exception is then thrown when you try to reference the null object by calling Trim on it.

The easiest way around this is to use Trim(strLastName) <> "" as a condition, or assigning the string trimmed in this way to itself and then testing just the variable. This function will just return an empty string if the argument is null.