I want to make sure that when doSomething() is called, the string variable is always populated (that it can't be left blank):

    function doSomething (string hash) public {
        require(hash != null);

Similarly, I want to make sure that when doSomethingElse() is called, the string is not already mapped to an address:

    mapping (string => address) public hashes;

    function doSomethingElse (string hash) public {
        require(hashes[hash] = null);

I know null is not the correct syntax, but what is?

2 Answers 2


in the first case I would check the string length

require(bytes(hash).length > 0);

in the second case just check against the void address

require(hashes[hash] == address(0x0));

Just to add to @mirg's valid answer.

As you know, in Solidity variables are never null. Instead, if they are not initialized, they contain their default value. For example for uint this is 0. So, whatever type you use, if it's not initialized its value is its default value.

This is a bit unclear when it comes to strings, but checking the bytes length seems to be the way to go. More answers for example here: How can you check if a `string` is empty in Solidity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.