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
187
votes
22 answers

Difference between null and empty ("") Java String

What is the difference between null and the "" (empty string)? I have written some simple code: String a = ""; String b = null; System.out.println(a == b); // false System.out.println(a.equals(b)); // false Both statements return false. It seems,…
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
185
votes
8 answers

JavaScript null check

I've come across the following code: function test(data) { if (data != null && data !== undefined) { // some code here } } I'm somewhat new to JavaScript, but, from other questions I've been reading here, I'm under the impression…
afsantos
  • 5,178
  • 4
  • 30
  • 54
184
votes
4 answers

How do I get SUM function in MySQL to return '0' if no values are found?

Say I have a simple function in MySQL: SELECT SUM(Column_1) FROM Table WHERE Column_2 = 'Test' If no entries in Column_2 contain the text 'Test' then this function returns NULL, while I would like it to return 0. I'm aware that a similar question…
Nick
  • 4,304
  • 15
  • 69
  • 108
184
votes
18 answers

Is it good practice to NULL a pointer after deleting it?

I'll start out by saying, use smart pointers and you'll never have to worry about this. What are the problems with the following code? Foo * p = new Foo; // (use p) delete p; p = NULL; This was sparked by an answer and comments to another question.…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
184
votes
19 answers

Cleaner way to do a null check in C#?

Suppose, I have this interface, interface IContact { IAddress address { get; set; } } interface IAddress { string city { get; set; } } class Person : IPerson { public IContact contact { get; set; } } class test { private test() …
181
votes
7 answers

What is the correct way to represent null XML elements?

I have seen null elements represented in several ways: The element is present with xsi:nil="true": Beowulf The element is present, but represented as an empty element (which I…
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
180
votes
21 answers

Why does NULL = NULL evaluate to false in SQL server

In SQL server if you have nullParam=NULL in a where clause, it always evaluates to false. This is counterintuitive and has caused me many errors. I do understand the IS NULL and IS NOT NULL keywords are the correct way to do it. But why does SQL…
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
178
votes
13 answers

How to check null objects in jQuery

I'm using jQuery and I want to check the existence of an element in my page. I have written following code, but it's not working: if($("#btext" + i) != null) { //alert($("#btext" + i).text()); $("#btext" + i).text("Branch " + i); } How do I…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
177
votes
14 answers

How to resolve TypeError: Cannot convert undefined or null to object

I've written a couple of functions that effectively replicate JSON.stringify(), converting a range of values into stringified versions. When I port my code over to JSBin and run it on some sample values, it functions just fine. But I'm getting this…
zahabba
  • 2,921
  • 5
  • 20
  • 33
177
votes
16 answers

When should I use nil and NULL in Objective-C?

This is sample code: NSDictionary *myDictionary = [NSDictionary dictionary]; NSNumber *myNumber = [myDictionary valueForKey: @"MyNumber"]; NSLog(@"myNumber = %@", myNumber); // output myNumber = (null) if (myNumber == nil) NSLog(@"test 1…
Biranchi
  • 16,120
  • 23
  • 124
  • 161
174
votes
6 answers

Why `null >= 0 && null <= 0` but not `null == 0`?

I had to write a routine that increments the value of a variable by 1 if its type is number and assigns 0 to the variable if not, where the variable is initially null or undefined. The first implementation was v >= 0 ? v += 1 : v = 0 because I…
Chungmin Lee
  • 2,320
  • 2
  • 18
  • 19
174
votes
7 answers

What are the advantages of using nullptr?

This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values…
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
173
votes
13 answers

Use of Boolean? in if expression

If I have a nullable Boolean b, I can do the following comparison in Java: Boolean b = ...; if (b != null && b) { /* Do something */ } else { /* Do something else */ } In Kotlin, I can achieve the same by using the !! operator: val b:…
nhaarman
  • 98,571
  • 55
  • 246
  • 278
173
votes
16 answers

Unique ways to use the null coalescing operator

I know the standard way of using the null coalescing operator in C# is to set default values. string nobody = null; string somebody = "Bob Saget"; string anybody = ""; anybody = nobody ?? "Mr. T"; // Returns Mr. T anybody = somebody ?? "Mr. T";…
170
votes
4 answers

SQL is null and = null

Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference between IS NULL and =NULL What is the difference between where x is null and where x = null and why does the latter not work?
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304