i was reminded the other day of some of the issues that can arise in code when an unexpected null value is encountered. this led me back to re-visit my understanding of null value handling in vfp and that is what this blog is about. let us begin by considering the various possible states in which a vfp value (whether it is a field in table, or a variable) can exist? in visual foxpro there are actually three conditions;
· the value can be equal to something (.t.)
· the value can be not equal to something (.f.)
· the value can be in a state where its value cannot be determined (null).
the first two are not a problem and we all deal with these scenarios daily in code. it is the third that can cause us problems unless we specifically plan for it. i often find it helpful, when thinking about null values to think of the null as meaning “i don’t know”. this helps to make sense of the behavior of nulls when they are encountered.
the first thing to remember about nulls is that the answer to any comparison involving a null value is always null. consider the following code:
luval = null
? luval > 0
? luval = "a character string"
? luval <= date()
? luval = .t.
the value returned in every case is actually null . in other words when vfp is asked how a null ( = i don’t know) relates to some other value, the answer is always null ( = i don’t know ). now you may be thinking at this point that this is a silly example, because you can always check the type of a variable (or field) using the type(), or vartype() functions.
unfortunately type() is definitely not recommended for checking for nulls because, although it will return a type of “l” for a variable that has been initialized as null like this:
luval = null
? type("luval") && returns ‘l’
it will return the original data type if the variable originally contained some non-null value and has since acquired a null, like this:
lntotal = 100
luval = 10
? type( ‘luval’ ) && type = “n”
luval = iif( luval > 20, luval, null )
? type( ‘luval’ ) && value = null but type = “n”
lntotal = lntotal + luval && lntotal = null - not even 100!!!
why is this so bad? consider what would happen if you were to be using code which accumulated a values inside a loop (e.g. a scan) and one record contained a null not only would your total so far be lost, but you would not even get the value back because as soon as you reached the null record the value in the accumulator would be set to null and all subsequent additions would simply end up as null you would not get an error, but you certainly would not get the result you desired.
vartype() is definitely better behaved in this respect. by default it returns a type of “x” if the tested value contains null, although, by passing the second parameter as .t. you can get the original data type (mimics the behavior of type() in other words).
in practice the best way to handle nulls is to test for them explicitly using either the isnull() function (that returns .t. if the tested value is null) or to wrap expressions that may contain nulls inside an nvl() so the any null is replaced with a more easily handled value. (in fact it was the omission of an nvl() that caused me grief this week and occasioned this blog).
nvl() is simply a function that substitutes a specified value when a null is encountered, like this:
luval = null
? nvl( luval, 0 ) && returns 0, not null!
fortunately you can use functions like sum() and the calculate functions, or an sql sum() even when null values are present. visual foxpro is intelligent enough to simply ignore such values - and indeed this is one of positive benefits of null support when calculating averages and counting records - nulls are ignored and do not affect the results.
having said all that, what was my problem? i forgot that when you are concatenating trimmed character strings, one of which contains a null, the result is null. the actual scenario that bit me was caused by a call to a sql server stored procedure that returned a value in a cursor. that value was supposed to be a space-padded character string containing up to six characters. this was to be trimmed, and used to generate a file name by concatenating it with the logged-in user id. something like this:
lcnextseq = alltrim( cur_result.cnextseq ) && this field was null
lcfilename = lcuserid + lcnextseq
the problem arose when the stored procedure returned null because a previous insert operation had failed and i had not wrapped the result in an nvl(). this did not generate an error, but the result was null. now this particular one is, i think, a bug (though it has been in the product since vfp 6.0 at least). the following snippet shows why:
lcval = alltrim(null)
? lcval && .null.
luval = null
? luval && .null.
? "test" + lcval && .null.
? "test" + luval && operator/operand mismatch!!!!
after all, if a mismatch arises when a character string is concatenated with one containing a null, then simply applying an alltrim() function should not allow the operation to proceed without error.
the moral of the story is, of course, to always plan for nulls!
incidentally you may be thinking that since you only use vfp data and you don’t allow nulls in your tables anyway, that this sort of thing is not going to happen. you will be right, so long as you don’t ever use an outer join in an sql query. remember that in such a query a null value is returned in the result set if there is no matching record – irrespective of whether your tables allow nulls or not.