Questions tagged [non-nullable]

232 questions
229
votes
11 answers

Best explanation for languages without null

Every so often when programmers are complaining about null errors/exceptions someone asks what we do without null. I have some basic idea of the coolness of option types, but I don't have the knowledge or languages skill to best express it. What is…
203
votes
4 answers

In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz. What is the idiomatic way of doing so in Kotlin? For example, this code is in error: val something: Xyz? = createPossiblyNullXyz() something.foo() //…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
59
votes
1 answer

Why @Nonnull annotation checked at runtime?

I have a function with following signature public static String myFunction(@Nonnull String param) When I call it with param as null, I get the following exception: Caused by: java.lang.IllegalArgumentException: Argument for @Nonnull parameter…
Dmitry
  • 1,056
  • 1
  • 8
  • 17
51
votes
3 answers

Create Non-Nullable Types in C#

How to create non-nullable value types like int, bool, etc. in C#?
Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
42
votes
2 answers

Best practice for using Nullable Reference Types for DTOs

I have a DTO which is populated by reading from a DynamoDB table. Say it looks like this currently: public class Item { public string Id { get; set; } // PK so technically cannot be null public string Name { get; set; } // validation to…
BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62
40
votes
6 answers

Why is null not allowed for DateTime in C#?

Why it is not allowed to assign null to a DateTime in C#? How has this been implemented? And can this feature be used to make your own classes non-nullable? Example: string stringTest = null; // Okay DateTime dateTimeTest = null; // Compile error I…
Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
35
votes
1 answer

How do I specify "any non-nullable type" as a generic type parameter constraint?

The post is specific to C# 8. Let's assume I want to have this method: public static TValue Get( this Dictionary src, TKey key, TValue @default ) => src.TryGetValue(key, out var value) ? value : @default; If my…
Alex Yakunin
  • 6,330
  • 3
  • 33
  • 52
28
votes
4 answers

Make Kotlin warn on assignment of flexible/platform type to non-null type?

When calling a non-nullability-annotated Java function from Kotlin, we get flexible-typed return values, denoted by exclamation marks, e.g. String!. Kotlin silently allows assigning these flexible values to a normal non-null type, e.g. String, which…
Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
28
votes
3 answers

Right way to use the @NonNull annotation in Android Studio

I'd like to use the @NonNull annotation in Android, but I can't figure out just the right way to do it. I propose you this example: public void doStuff(@NonNull String s){ //do work with s... } So when i call doStuff(null) the IDE will…
MMauro
  • 687
  • 2
  • 6
  • 14
28
votes
8 answers

C# creating a non-nullable string. Is it possible? Somehow?

So you can't inherit string. You can't make a non-nullable string. But I want to do this. I want a class, let's call it nString that returns a default value when it would otherwise be null. I have JSON objects that might have who knows how many null…
Jared Price
  • 5,217
  • 7
  • 44
  • 74
26
votes
7 answers

Making a Non-nullable value type nullable

I have a simple struct that has limited use. The struct is created in a method that calls the data from the database. If there is no data returned from the database I want to be able to return a null, but Visual Studio complains, Cannot convert null…
Malfist
  • 31,179
  • 61
  • 182
  • 269
23
votes
11 answers

What would we do without NULL?

I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article Anyway, so what if by default a language like C# used non-nullable types?…
Earlz
  • 62,085
  • 98
  • 303
  • 499
23
votes
2 answers

How can I get close to non-nullable reference types in C# today?

I've read many of the non-nullable questions and answers. It looks like the best way to get close to non-nullable types in C# (4.0) is Jon Skeet's NonNullable<> hack. However, it seems that C++/CLI has solved much of the problem by supporting…
Ðаn
  • 10,934
  • 11
  • 59
  • 95
20
votes
3 answers

Make a column nullable in DB2 when Data Capture is enabled

I'm using db2 version 9.7* and it seems impossible to make a NOT NULL column nullable in any straightforward way. Unfortunately the solution of using a more developer friendly database is not available. Basically, in MySQL speak, I want to do…
lukewm
  • 21,433
  • 6
  • 26
  • 28
17
votes
2 answers

Is constructor the only way to initialize non-nullable properties in a class in C#?

I have switched to enable nullable in my project that uses C#8. Now I have the following class: public class Request { public string Type { get; set; } public string Username { get; set; } public string Key { get; set; } } Compiler of…
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
1
2 3
15 16