mapping (address => uint) itemsPerCreator;

if (itemsPerCreator[_creator] == null){}

Remix give me a error: ParseError: Expected primary expression.

_creator is a addres type variable. Inside the mapping each addres have a uint value.

  • What is null? – goodvibration Dec 26 '18 at 9:28
  • The if is inside a function. So I´m trying to evaluate if is a value inside the mapping. That mapping have one a address and a uint value. So im evaluating if the value inside the mapping is null – UnexpectedCharacter Dec 26 '18 at 9:29
  • It doesn't answer my question - where have you declared a uint variable named null? – goodvibration Dec 26 '18 at 9:30
  • null is not a variable. Is a value – UnexpectedCharacter Dec 26 '18 at 9:31
  • 1
    Any unmapped value is actually mapped to the zero value of the given type, which in your case is uint(0), or simply 0. – goodvibration Dec 26 '18 at 9:50

You are getting this error because there is nothing like null or undefined in solidity. An uint initializes with 0.

So, to check the zero-state of value(either default or intentionally updated) to the corresponding key in your mapping, you should write it as:

if (itemsPerCreator[_creator] == 0){}

As per the Solidity documentation:

A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false. The default value for the uint or int types is 0. For statically-sized arrays and bytes1 to bytes32, each individual element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, bytes and string, the default value is an empty array or string.

| improve this answer | |
  • The part to check the non-existence of value to the corresponding key in your mapping is wrong. A value exists, and it is 0. In addition to that, any key mapped to 0 can also be added in an explicit manner. – goodvibration Dec 26 '18 at 10:18
  • @goodvibration You're right, answer updated. – Aniket Dec 26 '18 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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