Android Question Null and "" what is the best practice


Active Member
Licensed User
Hi, Guys
I am in the process of cleaning up some old code (which does work). This code appears to use null or "" relating to strings in indicate they are empty.
I remember reading a Post from Erel saying best not to use Null for some situations (but I can't find it) and I could have got it wrong.

I am looking for some comments on best practice for usage of Null and "".

1. If you want indicate a string is empty should it be?
Dim value as string
value = ""
' Or
value = Null
2. Likewise does the following give the same result?
' value is a string
IF value = Null then ....
' it it the same as
IF value = "" then

I think that empty strings should be set to ="" and null should not be used in relation to strings is this correct?

However for Objects, I have equated to Null (=Null) to indicate an object exists, and is initialized, but data has been cleared i.e.

Private thisObject as ObjectClass

'Code to insert data into the object and process it....
thisObject = Null ' indicates the object does not contain any information but can be used again.
' and
thisObject = "" '  is meaningless and should not be used.

Have I got it correct?


Brian Dean

Active Member
Licensed User
This is how I understand things. Doesn't mean that B4A/Java necessarily works this way.

An "object" variable is actually a memory pointer (reference) to where the object is stored in memory. You can have two object variables referencing the same memory space. If you set a pointer to Null it just points nowhere. The data that it was pointing to is not immediately destroyed, but will be in due course. But that doesn't matter, because there is no way back.

The same goes for a string. "SomeString = "" points to an empty string in memory; "SomeString = Null" points nowhere.

Anyway, that is how I think it works.
Upvote 0


B4X founder
Staff member
Licensed User
Never assign null to a string variable.
I consider a string variable that holds a null value a programming mistake. Nothing good will come out of this.

There are some isoteric cases where APIs returned null strings. SQL.ExecQuerySingleResult for example. This is considered a bug in the library that is not fixed to maintain backward compatibility.
Upvote 0


Active Member
Licensed User
I would agree with both these Posts regarding the use of null for strings and objects.

I think it is best to try to remove all the nulls out of my old code. Then use "" to clear out strings and ".initialize(..)" to clear objects.

Thanks for the input
Upvote 0


Licensed User
There are some isoteric cases where APIs returned null strings. SQL.ExecQuerySingleResult for example
I do not want to derail or detract from this thread, but I use this code even when items data are NULL and circumvent the limitation. If you are talking about something else, it would be interesting to give us an example.
Dim total As Double =Starter.SQL1.ExecQuerySingleResult("SELECT SUM(IFNULL(weight,0)) FROM dt_content")
Upvote 0