You need to sign in to do that
Don't have an account?

ternary operator issue in salesforce
What is the difference between the two statments?
Decimal total = 0;
Decimal amt;
total = total+amt==null?0:amt; // why does this statement throw "argument 1 cannot be null" exception
System.debug(total);
total = total+(amt = amt==null?0:amt);// why does this statement work
System.debug(total);
In salesforce all uninitalized variables are set with null. you can not apply addition + operator on null values. In your example you have not initalized amt which is null.
In the statement that is working you checking if it is null and if you assigning 0 to it so it works.
In the first example, it is interpreted as:
This fails, because amt is null, and you can't add a null value to another value.
The second one works because amt is first compared to null, and returns 0 if so before attempting to add to total. You could also make this code shorter: