Questions tagged [null]

Null means *nothing* or *unknown*, depending on context. Please use the "sql-null" tag for SQL specific questions.

"Null" has different meanings depending on context.

Set theory

Null is another name for the empty set, denoted by the symbol ∅. For this reason, some programming languages use null or nil for the empty list or empty tuple. Lisp uses nil to mean the empty list and the boolean value false.

Pointers and References

Null (or sometimes nil or None), in programming languages that support it, is the value of an uninitialized variable, a pointer that doesn't point to a meaningful memory address, or an object that fails to respond to any message.

Nullable references were invented by C.A.R. Hoare in 1965 as part of the Algol W language. Hoare later described his invention as a "billion-dollar mistake".

For more information, see Null pointer.

Agreeing with Hoare's sentiment, many languages don't have a special null value, choosing to use optional types instead.

Relational Databases

NULL as a special marker in SQL or a relational database stands in place of a value that is missing, or in a join means "no corresponding row." The operators IS NULL and IS NOT NULL are required for comparisons to a literal null: other comparisons between NULL and any value evaluate to "unknown."

For more information, see Null (SQL).

ASCII

Null or NUL is the name given to the character with ASCII code zero (0) - i.e. hex 00. In some languages, notably C, NUL is used to mark the end of a character string.

For more information, see Null character.

16208 questions
144
votes
7 answers

How to remove all the null elements inside a generic list in one go?

Is there a default method defined in .Net for C# to remove all the elements within a list which are null? List parameterList = new List{param1, param2, param3...}; Let's say some of the parameters are null;…
pencilCake
  • 51,323
  • 85
  • 226
  • 363
144
votes
8 answers

Check for null in foreach loop

Is there a nicer way of doing the following: I need a check for null to happen on file.Headers before proceeding with the loop if (file.Headers != null) { foreach (var h in file.Headers) { //set lots of properties & some other stuff …
Eminem
  • 7,206
  • 15
  • 53
  • 95
142
votes
24 answers

Is returning null bad design?

I've heard some voices saying that checking for a returned null value from methods is bad design. I would like to hear some reasons for this. pseudocode: variable x = object.method() if (x is null) do something
koen
  • 13,349
  • 10
  • 46
  • 51
141
votes
11 answers

Method call if not null in C#

Is it possible to somehow shorten this statement? if (obj != null) obj.SomeMethod(); because I happen to write this a lot and it gets pretty annoying. The only thing I can think of is to implement Null Object pattern, but that's not what I can…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
141
votes
3 answers

What does exclamation mark mean before invoking a method in C# 8.0?

I have found a code written in C# seemingly version 8.0. In the code, there is an exclamation mark before invoking a method. What does this part of the code mean, and above all, what are its uses? var foo = Entity!.DoSomething();
icn
  • 1,783
  • 2
  • 7
  • 13
140
votes
4 answers

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

according to the documentation, the method String.valueOf(Object obj) returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned. But how come when I try do…
user282886
  • 3,125
  • 8
  • 22
  • 11
139
votes
6 answers

Avoid division by zero in PostgreSQL

I'd like to perform division in a SELECT clause. When I join some tables and use aggregate function I often have either null or zero values as the dividers. As for now I only come up with this method of avoiding the division by zero and null values.…
William Wino
  • 3,599
  • 7
  • 38
  • 61
138
votes
5 answers

How much size "Null" value takes in SQL Server

I have a large table with say 10 columns. 4 of them remains null most of the times. I have a query that does null value takes any size or no size in bytes. I read few articles some of them are saying…
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
137
votes
12 answers

Shortest way to check for null and assign another value if not

I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null. I'm currently doing it like this: if (string.IsNullOrEmpty(planRec.approved_by) == true) this.approved_by = ""; else …
Splashlin
  • 7,225
  • 12
  • 46
  • 50
135
votes
16 answers

Deep null checking, is there a better way?

Note: This question was asked before the introduction of the .? operator in C# 6 / Visual Studio 2015. We've all been there, we have some deep property like cake.frosting.berries.loader that we need to check if it's null so there's no exception. The…
Homde
  • 4,246
  • 4
  • 34
  • 50
133
votes
11 answers

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

This is valid C# code var bob = "abc" + null + null + null + "123"; // abc123 This is not valid C# code var wtf = null.ToString(); // compiler error Why is the first statement valid?
superlogical
  • 14,332
  • 9
  • 66
  • 76
132
votes
3 answers

Mongo: find items that don't have a certain field

How to search for documents in a collection that are missing a certain field in MongoDB?
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
132
votes
8 answers

error: ‘NULL’ was not declared in this scope

I get this message when compiling C++ on gcc 4.3 error: ‘NULL’ was not declared in this scope It appears and disappears and I don't know why. Why? Thanks.
jackhab
  • 17,128
  • 37
  • 99
  • 136
130
votes
2 answers

Sort NULL values to the end of a table

Is there a way with PostgreSQL to sort rows with NULL values in fields to the end of the selected table? Like: SELECT * FROM table ORDER BY somevalue, PUT_NULL_TO_END
helle
  • 11,183
  • 9
  • 56
  • 83
129
votes
13 answers

Unable to cast object of type 'System.DBNull' to type 'System.String`

I got the above error in my app. Here is the original code public string GetCustomerNumber(Guid id) { string accountNumber = (string)DBSqlHelperFactory.ExecuteScalar(connectionStringSplendidmyApp, …
Saif Khan
  • 18,402
  • 29
  • 102
  • 147