Null check chain vs catching NullPointerException

Catching NullPointerException is a really problematic thing to do since they can happen almost anywhere. It’s very easy to get one from a bug, catch it by accident and continue as if everything is normal, thus hiding a real problem. It’s so tricky to deal with so it’s best to avoid altogether. (For example, think … Read more

Is null an Object?

If null were an Object, it would support the methods of java.lang.Object such as equals(). However, this is not the case – any method invocation on a null results in a NullPointerException. And this is what the Java Language Specification has to say on this topic: There is also a special null type, the type … Read more

Method call if not null in C#

From C# 6 onwards, you can just use: MyEvent?.Invoke(); or: obj?.SomeMethod(); The ?. is the null-propagating operator, and will cause the .Invoke() to be short-circuited when the operand is null. The operand is only accessed once, so there is no risk of the “value changes between check and invoke” problem. === Prior to C# 6, … Read more

Why is it Valid to Concatenate Null Strings but not to Call “null.ToString()”?

The reason for first one working: From MSDN: In string concatenation operations,the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string. More information on the + binary operator: The binary + operator performs string concatenation when one or both operands … Read more

Deep null checking, is there a better way?

We have considered adding a new operation “?.” to the language that has the semantics you want. (And it has been added now; see below.) That is, you’d say cake?.frosting?.berries?.loader and the compiler would generate all the short-circuiting checks for you. It didn’t make the bar for C# 4. Perhaps for a hypothetical future version … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)