Questions tagged [null]

Null is the absence of a value. Null is typically used to indicate that a reference or pointer variable points to no object in memory.

From Wikipedia:

A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers are routinely used to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

125 questions
178
votes
22 answers

Are null references really a bad thing?

I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake". But why? Sure, they can cause NullReferenceExceptions, but so what? Any element of the language can be a source of errors if used…
Tim Goodman
  • 2,644
  • 2
  • 28
  • 25
126
votes
16 answers

Should one check for null if he does not expect null?

Last week, we had a heated argument about handling nulls in our application's service layer. The question is in the .NET context, but it will be the same in Java and many other technologies. The question was: should you always check for nulls and…
Stilgar
  • 1,504
  • 2
  • 10
  • 13
91
votes
11 answers

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

AFAIK, Option type will have runtime overhead, while nullable types won't, because Option time is an enum (consuming memory). Why not just mark optional references as optional, then the compiler can follow code execution and find whenever it can't…
Chayim Friedman
  • 1,138
  • 1
  • 6
  • 14
91
votes
10 answers

If null is bad, why do modern languages implement it?

I'm sure designers of languages like Java or C# knew issues related to existence of null references (see Are null references really a bad thing?). Also implementing an option type isn't really much more complex than null references. Why did they…
zduny
  • 2,623
  • 2
  • 19
  • 24
81
votes
12 answers

SQL: empty string vs NULL value

I know this subject is a bit controversial and there are a lot of various articles/opinions floating around the internet. Unfortunatelly, most of them assume the person doesn't know what the difference between NULL and empty string is. So they tell…
Jacek Prucia
  • 2,264
  • 1
  • 17
  • 15
75
votes
7 answers

How does a surname of Null cause problems in many databases?

I read an article on BBC. One of the examples they said was that people with surname 'Null' are having problems with entering their details in some websites. No explanation is given about the error they are facing. But as far as I know the string…
Nitish
  • 779
  • 1
  • 5
  • 9
60
votes
10 answers

If null is a billion dollar mistake, what is the solution to represent a non-initialized object?

This comes with a debate with my colleague that I'm using null as an object initial state. type Value = Node | null const [value, setValue] = React.useState(null) function test(v: Value) { if (value === null) // Do something else // Do…
Mengo
  • 579
  • 1
  • 4
  • 7
60
votes
24 answers

How can I explain the difference between NULL and zero?

Working on a problem that uses the percent change formula: percent change = 100 * [(new value - old value) / old value] How would I explain the difference if new value or old value = NULL, rather than 0 to someone who might not be a programmer? My…
O.O
  • 668
  • 1
  • 9
  • 15
53
votes
6 answers

How do languages with Maybe types instead of nulls handle edge conditions?

Eric Lippert made a very interesting point in his discussion of why C# uses a null rather than a Maybe type: Consistency of the type system is important; can we always know that a non-nullable reference is never under any circumstances observed…
Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
46
votes
9 answers

What's wrong with returning null?

I've recenlty been greeted by CS8603 - Possible null reference return, which indicates that my code could possibly return null. It's a simple function that looks up an entity in a database by id - if it exists, it returns the entity. If not, it…
MechMK1
  • 659
  • 1
  • 5
  • 13
45
votes
8 answers

Why doesn't "object reference not set to an instance of an object" tell us which object?

We're launching a system, and we sometimes get the famous exception NullReferenceException with the message Object reference not set to an instance of an object. However, in a method where we have almost 20 objects, having a log which says an…
Saeed Neamati
  • 18,142
  • 23
  • 87
  • 125
44
votes
6 answers

Where are null values stored, or are they stored at all?

I want to learn about null values or null references. For example I have a class called Apple and I created an instance of it. Apple myApple = new Apple("yummy"); // The data is stored in memory Then I ate that apple and now it needs to be null, so…
Mert Akcakaya
  • 2,089
  • 1
  • 13
  • 16
40
votes
8 answers

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

For example, suppose I have a class, Member, which has a lastChangePasswordTime: class Member{ . . . constructor(){ this.lastChangePasswordTime=null, } } whose lastChangePasswordTime can be meaningful absent, because some members may…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
35
votes
14 answers

If nulls are evil, what should be used when a value can be meaningfully absent?

This is one of the rules that are beaing repeated over and over and that perplex me. Nulls are evil and should be avoided whenever possible. But, but - from my naivety, let me scream - sometimes a value CAN be meaningfully absent! Please let me ask…
gaazkam
  • 3,517
  • 3
  • 19
  • 35
31
votes
7 answers

Should I validate a method call's return value even if I know that the method can't return bad input?

I'm wondering if I should defend against a method call's return value by validating that they meet my expectations even if I know that the method I'm calling will meet such expectations. GIVEN User getUser(Int id) { User temp = new User(id); …
Didier A.
  • 1,327
  • 14
  • 10
1
2 3
8 9