In C#, what happens when you call an extension method on a null object?

That will work fine (no exception). Extension methods don’t use virtual calls (i.e. it uses the “call” il instruction, not “callvirt”) so there is no null check unless you write it yourself in the extension method. This is actually useful in a few cases: public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } public … Read more

Should a retrieval method return ‘null’ or throw an exception when it can’t produce the return value? [closed]

If you are always expecting to find a value then throw the exception if it is missing. The exception would mean that there was a problem. If the value can be missing or present and both are valid for the application logic then return a null. More important: What do you do other places in … Read more

IllegalArgumentException or NullPointerException for a null parameter? [closed]

You should be using IllegalArgumentException (IAE), not NullPointerException (NPE) for the following reasons: First, the NPE JavaDoc explicitly lists the cases where NPE is appropriate. Notice that all of them are thrown by the runtime when null is used inappropriately. In contrast, the IAE JavaDoc couldn’t be more clear: “Thrown to indicate that a method … Read more

How to filter empty or NULL names in a QuerySet?

You could do this: Name.objects.exclude(alias__isnull=True) If you need to exclude null values and empty strings, the preferred way to do so is to chain together the conditions like so: Name.objects.exclude(alias__isnull=True).exclude(alias__exact=””) Chaining these methods together basically checks each condition independently: in the above example, we exclude rows where alias is either null or an empty string, … Read more

Representing null in JSON

Let’s evaluate the parsing of each: http://jsfiddle.net/brandonscript/Y2dGv/ var json1 = ‘{}’; var json2 = ‘{“myCount”: null}’; var json3 = ‘{“myCount”: 0}’; var json4 = ‘{“myString”: “”}’; var json5 = ‘{“myString”: “null”}’; var json6 = ‘{“myArray”: []}’; console.log(JSON.parse(json1)); // {} console.log(JSON.parse(json2)); // {myCount: null} console.log(JSON.parse(json3)); // {myCount: 0} console.log(JSON.parse(json4)); // {myString: “”} console.log(JSON.parse(json5)); // {myString: “null”} … Read more

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