Check for null in foreach loop

Just as a slight cosmetic addition to Rune’s suggestion, you could create your own extension method: public static IEnumerable<T> OrEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } Then you can write: foreach (var header in file.Headers.OrEmptyIfNull()) { } Change the name according to taste 🙂

MongoDB: How to query for records where field is null or not set?

If the sent_at field is not there when its not set then: db.emails.count({sent_at: {$exists: false}}) If it’s there and null, or not there at all: db.emails.count({sent_at: null}) If it’s there and null: db.emails.count({sent_at: { $type: 10 }}) The Query for Null or Missing Fields section of the MongoDB manual describes how to query for null … Read more

Why does String.valueOf(null) throw a NullPointerException?

The issue is that String.valueOf method is overloaded: String.valueOf(Object) String.valueOf(char[]) Java Specification Language mandates that in these kind of cases, the most specific overload is chosen: JLS 15.12.2.5 Choosing the Most Specific Method If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to … Read more

Java null check why use == instead of .equals()

They’re two completely different things. == compares the object reference, if any, contained by a variable. .equals() checks to see if two objects are equal according to their contract for what equality means. It’s entirely possible for two distinct object instances to be “equal” according to their contract. And then there’s the minor detail that … Read more

Does free(ptr) where ptr is NULL corrupt memory?

7.20.3.2 The free function Synopsis #include <stdlib.h> void free(void *ptr); Description The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. See ISO-IEC 9899. That being said, when looking at different codebases in the wild, you’ll … Read more

Is it better to return `undefined` or `null` from a javascript function?

Undefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined. From the ECMAScript2015 spec 4.3.10 undefined value primitive value used when … Read more

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