Questions tagged [nullable]

The nullable tag is for issues relating to nullable members or types. A null is used to represent a missing or unknown value.

A data member is nullable if it can have the special value of NULL, instead of their common range of possible values. Nullable types are a feature of some statically-typed programming languages.

.NET

In , the built-in generic type System.Nullable<T> allows the programmer to use value types like int or DateTime with a value of null. Several .NET languages have a special syntax for declaring Nullable:

  • In , int? is an alias for System.Nullable<System.Int32>
  • In , Integer? is an alias for System.Nullable(Of System.Int32)

SQL

Null is a special marker used in SQL to indicate that data value do not exist in the database. Its proper use ensures various statistical aggregate functions perform correctly.

See more on Wikipedia.

D

The typecons module in package std has a templated struct - Nullable - for wrapping values types and allowing them to appear to have a null value (as in C#). Restriction is that the type must be able to be instantiated with T var or T var = T.init, where T represents the type being used.

Related tags

2417 questions
15
votes
4 answers

Make An Integer Null

I have an update function that updates an sql server db table through a dataset. One of the fields in the table is an integer and accepts null values. So when I am populating the update function I need a way to enter a null in when the function…
Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152
15
votes
2 answers

How to use Kotlin's `with` expression for nullable types

The code below will not compile because the variable myType can be null. Is there a way of executing a with block for nullable types in Kotlin? val myType: MyType? = null with(myType) { aMethodThatBelongsToMyType() …
memoizr
  • 2,081
  • 2
  • 18
  • 24
15
votes
4 answers

Parsing value into nullable enumeration

Let's say I have this: PriorityType? priority; string userInput = ...; I cannot change how this is defined: PriorityType? priority because it's actually part of a contract with another piece of code. I tried this, but it does not work: if…
core
  • 32,451
  • 45
  • 138
  • 193
15
votes
2 answers

Varchar columns: Nullable or not

The database development standards in our organization state the varchar fields should not allow null values. They should have a default value of an empty string (""). I know this makes querying and concatenation easier, but today, one of my…
DCNYAM
  • 11,966
  • 8
  • 53
  • 70
15
votes
7 answers

Empty string in not-null column in MySQL?

I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff from PHP. Lately I've been switching over to using the wonderful MDB2 class. Along with it, I'm using prepared statements, so I don't have to worry about…
davr
  • 18,877
  • 17
  • 76
  • 99
15
votes
1 answer

How scala generic constraints to nullable types work

I've tried two ways to constrain a generic type parameter to a nullable type, but both seem to have some unexpected problems. First attempt (using T <: AnyRef): scala> def testAnyRefConstraint[T <: AnyRef](option:Option[T]):T = { | //without…
Dan Shryock
  • 345
  • 2
  • 9
15
votes
2 answers

Converting a Guid to Nullable Guid

Is this an idiomatic way to convert a Guid to a Guid?? new Guid?(new Guid(myString));
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
15
votes
4 answers

Why are Nullable considered a value type?

Have you ever tried to use the Convert.ChangeType() method to convert a value to a Nullable type? Awkwardly, it will throw an InvalidCastException saying "Null object cannot be converted to a value type". Try running this on your immediate…
jpbochi
  • 4,366
  • 3
  • 34
  • 43
15
votes
3 answers

Workaround for "null primitives" in JDBC PreparedStatement?

When using raw JDBC, you can parameterize a PreparedStatement like so: PreparedStatement statement = connection.prepareStatement(someSQLString); String someString = getSomeString(); Integer int = getSomeInteger(); statement.setString(1,…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
15
votes
1 answer

Hibernate default joining for nullable many-to-one

I have a hibernate mapping like this in a ProductDfn class @ManyToOne( fetch = FetchType.LAZY, optional = true ) @JoinColumn( name = "productTypeFk", nullable = true ) public ProductType getProductType() { return productType; } Note that the…
Mike Q
  • 22,839
  • 20
  • 87
  • 129
15
votes
5 answers

How to make a linq Sum return null if the summed values are all null

I have a LINQ query that looks like this... var duration = Level3Data.AsQueryable().Sum(d => d.DurationMonths); If all the d.DurationMonths values are null the Sum returns 0. How can I make the Sum return null if all the d.DurationMonths are null?…
Simon Keep
  • 9,886
  • 9
  • 63
  • 78
15
votes
4 answers

MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key

Our MySQL web analytics database contains a summary table which is updated throughout the day as new activity is imported. We use ON DUPLICATE KEY UPDATE in order that the summarization overwrites earlier calculations, but are having difficulty…
ryandenki
  • 1,859
  • 3
  • 19
  • 30
15
votes
4 answers

Extension method for nullable enum

I'm trying to write an Extension method for nullable Enums. Like with this example: // ItemType is an enum ItemType? item; ... item.GetDescription(); So I wrote this method which doesn't compile for some reason that I don't understand: public…
gdoron
  • 147,333
  • 58
  • 291
  • 367
14
votes
5 answers

How to change what default(T) returns in C#?

I would like to change how default(T) behaves for certain classes. So instead of returning null for my reference types I would like to return a null object. Kind of like kids.Clear(); var kid = kids.Where(k => k.Age < 10).SingleOrDefault(); if…
Jan Ohlson
  • 215
  • 4
  • 7
14
votes
5 answers

MySQL Question - Unique Key Not functioning correctly, or am I misunderstanding?

I'm trying to create a relation where any of four different parts may be included, but any collection of the same parts should be handled as unique. Example: An assignment must have an assigned company, may optionally have an assigned location,…
Drew Arrigoni