Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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.

Filter by
Sorted by
Tagged with
45 votes
9 answers
13k views

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 ...
user avatar
  • 629
-2 votes
1 answer
68 views

What do you call a function that properly remains undefined?

Backstory: I have subclasses that are supposed to override and define various functions, but not neccessarily all of them. They can't remain purely virtual though for obvious reasons, and so I am ...
user avatar
  • 3,319
-1 votes
2 answers
334 views

Determining the object that caused a null reference exception?

Frequently in applications we encounter situations that could throw a NullReferenceException; for example, assuming the following method's argument is a user defined reference type, accessing the ...
user avatar
-2 votes
3 answers
298 views

Why `struct` unconstrained method fails to accept `null` as `T?` parameter

Considering example below, one observe that for reference type argument is successfully understood as nullable parameter. For value type conversion to T?/Nullable<T> fails. T Method<T> ( T?...
user avatar
  • 268
0 votes
2 answers
132 views

DDD Modeling - Entity with generic and specific data

Suppose I am buying coffee. There are several types of coffee (A1, A2, A3), but sometimes I want to make a reference to all types of coffee (like if I had a coffee "grouped"). Considering ...
user avatar
4 votes
1 answer
2k views

What are the advantages of checking for objects with Objects.nonNull()?

Our project changed the object existence checks from foo == null or foo != null to Objects.isNull(foo) or Objects.nonNull(foo). I never got an explanation why that should be better, that's why I ask. ...
user avatar
0 votes
1 answer
108 views

Use nullable or undefineable as object type?

This come with a debate with my colleague that I'm using nullable object type. type Value = Node | null const [v0, setV0] = React.useState<Value>(null) const [v1, setV1] = React.useState&...
user avatar
  • 539
56 votes
10 answers
10k views

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<Value>(null) function test(v: Value) ...
user avatar
  • 539
1 vote
1 answer
162 views

What is the reasoning behind Kotlin using non-nullable types for Java interop methods?

Considering Kotlin Java Interop: Null Safety and Platform Types Why is code like this legal in Kotlin? fun envString(key: EnvVars): String { return System.getenv(key.toString()) } getenv() can ...
user avatar
  • 589
1 vote
5 answers
628 views

Is this an anti pattern or misusing the interface default method?

Instead of checking the null and throwing exception each time we call findByOrderNumber method, I came up with this pattern by taking advantage of a default method, are there any patterns misused here ...
user avatar
89 votes
11 answers
18k views

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 ...
user avatar
0 votes
3 answers
244 views

How to inherit a static function in a class?

Probably the answer is you can't. However, I would like to have a work-around to solve my problem. Objective I am trying to create a program in which I try to avoid nulls as much as possible. ...
user avatar
3 votes
5 answers
3k views

Java: Why not allow nulls in methods to represent optional parameters?

I wanted to follow up on this previous question I asked related to @Laive comment, but I couldn't think of an excellent way to do so without asking another question, so here we go. With the previous ...
user avatar
  • 187
3 votes
4 answers
280 views

Should a transformation function take a nullable and return a nullable or should the caller handle nullability

Often I need to transform a type to another, such as a networking model to a data model, or a data model to a binary representation. Should these transformation functions take an Optional/nullable ...
user avatar
1 vote
1 answer
117 views

declare 2 classes for nullable / not nullable objects?

Let's say I have an which is loosely can be represented as: public class AnObject{ public AnObject(String name, String value, UUID id) { this.name = Objects.requireNonNull(...
user avatar
  • 113
1 vote
1 answer
347 views

How to handle adding new field / partial data update for a gRPC API?

Assuming we have following message that will be used to update data and it just got updated to version 2. message HelloRequest { string name = 1; // version 1 bool is_valid = 2; // version 2 } ...
user avatar
0 votes
0 answers
158 views

Should I check for nulls, even though I won't get any, as far as I know? [duplicate]

I'm new to software engineering and right now I'm focused on learning the best practices to consistently write robust code. Recently I've been maintaining an application built by other people and/or ...
user avatar
-1 votes
3 answers
196 views

When to use a reserved word, null or undefined as a key in an object?

I have a case where I have a dictionary object and a value with no key. The object can have the system values and then a user value. I have to store that value. I could use a reserved word or I ...
user avatar
40 votes
8 answers
10k views

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 ...
user avatar
  • 5,572
25 votes
3 answers
3k views

C# 8 non-nullable references and the Try pattern

There is a pattern in C# classes exemplified by Dictionary.TryGetValue and int.TryParse: a method that returns a boolean indicating success of an operation and an out parameter containing the actual ...
user avatar
-2 votes
1 answer
476 views

Using static member methods to check for object being NULL

Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse. Something like, #include <...
user avatar
  • 105
8 votes
1 answer
5k views

Is there a convention for the Optional/Maybe monad in typescript?

Let's take the classical example of a function that may return a number or not. In typescript this can be represented like this: function f(): number | undefined {} A more elaborate way would be to ...
user avatar
-1 votes
2 answers
175 views

Null checks good practice if code will only be ran when object is never null

Using a website with Javascript as example. Let's say I have script A which only performs a specific function on page Foo. For example something like sorting elements in a list. Script A is only ...
user avatar
  • 109
-2 votes
2 answers
196 views

How to have zero or one of and object in a class? [closed]

I want a class A, and I want the relation that A can have zero or one X. So I wrote this class: public class A { private X x = null; public A() {} public A(X x) { this.x = x; } ...
user avatar
  • 1,398
0 votes
2 answers
1k views

Is it beneficial to throw a caught IOException as an UncheckedIOException in order to prevent NullPointerException?

Not using UncheckedIOException, NullPointerException possible public void callerMethod() { Object result = ioMethod(); // call instance method of result } public Object ioMethod() { ...
user avatar
1 vote
1 answer
168 views

How to set nullable/optional property of database object to NULL in backend?

We are building API which mainly passes database objects back and forth between user and database, so the main flow of information is quite basic: Table (view) <-- ORM --> C#/Java/etc. Objects &...
user avatar
3 votes
1 answer
209 views

if null is bad how we justify the "rfc" nullable for php language?

I do not understand how you think about it:https://wiki.php.net/rfc/nullable_types when It is widely confirmed, that using nulls is bad practice Where am I wrong? thanks. I'm not criticizing !. I ...
user avatar
1 vote
3 answers
3k views

Setter with one parameter and null default value

Which solution is most logical? The value can be null, but when not null it must be a string. This (First): function setValue(string $value = null); To me this is bad; since we can now call the ...
user avatar
  • 87
34 votes
13 answers
5k views

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 ...
user avatar
  • 3,317
2 votes
3 answers
9k views

How to avoid != null statements? [duplicate]

I use code object != null to avoid NullPointerException. Is there a good alternative to solve this as follow ? if (someobject != null) { someobject.doCalc(); } This will not work for ...
user avatar
3 votes
3 answers
2k views

As API author, should I treat Empty and Null the same in search criteria?

I have a RESTFUL api, one of the endpoints is receiving search criteria which contains property for "Title". Should I allow consumers to send either null (or eliminate the property) or Empty string in ...
user avatar
  • 270
0 votes
2 answers
1k views

Should I throw ArgumentNullException and should I test it? [duplicate]

Let's take an example of class with 3 dependencies and one method. class Example { private readonly IDependency1 _d1; private readonly IDependency2 _d2; private readonly IDependency3 _d3;...
user avatar
-1 votes
1 answer
275 views

Null Object and Exceptions

Do Special Case or Null Object design patterns still provide value when application behavior, not just object behavior has to change? I was tasked with revisiting an old application and refactoring ...
user avatar
8 votes
8 answers
786 views

Is there a compelling reason why columns in SQL are nullable by default?

As a CS student, I've learned a decent number of programming languages over the years, most of which have had some concept of a "nullable" or "optional" type. Note that I'm not talking about null ...
user avatar
2 votes
3 answers
187 views

Is there anything wrong with making nullability of a reference type explicit by wrapping it in a value type?

When I program in Java, I make all nullability explicit; that is, an instance of Foo is assumed to be non-null, and if I want it to be null, I use a @Nullable annotation (or better, Optional<Foo>...
user avatar
5 votes
4 answers
782 views

Return NULL in case of success

This question is a clearer version of a question I posted on SO. I have a C++ Planner object with a method that computes a Route from a start point to a destination point. Planner is the owner of the ...
user avatar
  • 159
1 vote
2 answers
227 views

Setting a pointer to NULL on failure?

I've been modifying some code written by a previous employee and came across a function with the following signature: BOOL WINAPI PrependPadding( _In_ SIZE_T cbPadding, _In_ SIZE_T cbRow, ...
user avatar
3 votes
4 answers
274 views

Advice for bugfixing object oriented code: why is data not set?

When debugging object oriented (Java) code, sometimes some field of some object is null while it should not be. I often spend what I feel is way too much time trying to figure out where it should have ...
user avatar
  • 642
1 vote
1 answer
270 views

Setters and checking on null value

We are dealing with a lot of null values, now and we are wondering if there are any standards for avoiding it at some places in the code. For example I have this PHP code: class X { ...
user avatar
  • 87
2 votes
0 answers
382 views

What programming language first used the 'Safe navigation operator' (?.)?

What programming language first used / invented the 'Safe navigation operator' (?.) ? Unfortunately, the Wikipedia page on this subject does not have information on it and googling yielded no readily ...
user avatar
  • 136
12 votes
1 answer
463 views

What are some intuitions that support calling the Maybe constructor in Haskell "Just"?

The intuition of an optional type like Maybe Int is that either there is no Int (thus, there's Nothing there) or that there is some Int; there is something there. It makes sense to me that we call the ...
user avatar
  • 276
1 vote
2 answers
7k views

Unit tests for constructors

If I make a class, that in the constructor accepts an array or collection, but this array or collection is optional (the parameter can be null), and then I store that collection in the object in ...
user avatar
2 votes
6 answers
2k views

When do the benefits of nullable value types outweigh the cost of null pointers?

The question is in the title. Here is the context: Some people think that the null pointer is a big mistake. Tony Hoare famously apologized for inventing it. Since version 2.0 C# has had nullable ...
user avatar
17 votes
4 answers
8k views

Fetching a value without having to null check in Java

Many times I find myself null checking when fetching a value from some data hierarchy to avoid NullPointerExceptions, which I find to be prone to errors and a needs a lot of boilerplate. I've written ...
user avatar
5 votes
1 answer
4k views

Is it possible to make nonnull become part of C++ type system?

NULL is the billion-dollar mistake but there is nothing in the type system of C++ to prevent it. However, C++ already has const-correctness so implementing NULL-correctness seems trivial: introduce a ...
user avatar
1 vote
5 answers
1k views

What value does the byte pointed by null pointer have?

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL. However, the memory cell at that address does exists after all, right? In that case, ...
user avatar
  • 129
1 vote
2 answers
175 views

Should I return true in a method that casts null from one object type to another?

I'm writing a very simple extension method that attempts to cast objects from one type to another. The intent of having this method is very similar to Int32.TryParse(string, out int), which allows the ...
user avatar
  • 188
2 votes
1 answer
362 views

What native JavaScript functions/methods/properties/operators can return null? undefined? [closed]

I'm writing some code that needs to deal with many potential JavaScript values including so-called non-values . I'm realizing it would be very helpful to know if there are some general rules about ...
user avatar
2 votes
3 answers
1k views

Benefit of throwing exceptions for null parameters

The codebase I work with has a certain pattern prevalent in all public methods, which goes like this: public void UpdateUser(User userArg) { Framework.NullCheck(userArg); var user = userDb....
user avatar
  • 281
5 votes
0 answers
13k views

What should I use for @Nullable if I used the lombok @NonNull?

I am using the lombok @NonNull annotation for some of my fields, parameters and methods, partly for null checking, and also partly for documentation purpose. It is then confusing if I added a @NonNull/...
user avatar
  • 618