Questions tagged [null-check]

A check to see that an object has been instantiated.

A check to see that an object has been instantiated.

Typically, in a OO programming language, this is to see whether an object has been created or not.

e.g (Java example)

public void doStuff(Object obj) {
    if (obj != null) {
        // Do stuff with the object
    }
}
217 questions
706
votes
28 answers

Is there a way to check for both `null` and `undefined`?

Since TypeScript is strongly-typed, simply using if () {} to check for null and undefined doesn't sound right. Does TypeScript have any dedicated function or syntax sugar for this?
David Liu
  • 16,374
  • 12
  • 37
  • 38
365
votes
10 answers

How to check if a variable is not null?

I know that below are the two ways in JavaScript to check whether a variable is not null, but I’m confused which is the best practice to use. Should I do: if (myVar) {...} or if (myVar !== null) {...}
nickb
  • 9,140
  • 11
  • 39
  • 48
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
97
votes
11 answers

How to check whether all properties of an object are null or empty?

I have an object, lets call it ObjectA, and that object has 10 properties that are all strings. var myObject = new {Property1="",Property2="",Property3="",Property4="",...} Is there anyway to check to see whether all these properties are null or…
akd
  • 6,538
  • 16
  • 70
  • 112
43
votes
2 answers

Comparing a generic against null that could be a value or reference type?

public void DoFoo(T foo) where T : ISomeInterface { //possible compare of value type with 'null'. if (foo == null) throw new ArgumentNullException("foo"); } I'm purposely only checking against null because I don't want to restrict a…
michael
  • 14,844
  • 28
  • 89
  • 177
35
votes
3 answers

How to check for null value inline and throw an error in typescript?

In C# I can write code to check for null references and in case throw a custom exception, example: var myValue = (someObject?.SomeProperty ?? throw new Exception("...")).SomeProperty; In recent updates TypeScript introduced the null coalescing…
Valentino Miori
  • 477
  • 1
  • 7
  • 16
33
votes
3 answers

Null pointer check via "myPtr > 0"

In some legacy code I came across the following null pointer check. if( myPtr > 0 ) { ... } Are there any technical risks of checking for a null pointer via this if-check?
ucczs
  • 609
  • 5
  • 15
33
votes
4 answers

C#: How to perform a null-check on a dynamic object

How do I perform a null-check on a dynamic object? Pseudo code: public void Main() { dynamic dynamicObject = 33; if(true) { // Arbitrary logic dynamicObject = null; } Method(dynamicObject); } public void Method(dynamic…
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
28
votes
1 answer

Can a Java varargs be null?

I found myself checking for this and asking if it's necessary. I have code like this: public Object myMethod(Object... many) { if (many == null || many.length == 0) return this; for (Object one : many) doSomethingWith(one); return…
Ky -
  • 30,724
  • 51
  • 192
  • 308
25
votes
9 answers

Null or empty check for a string variable

if((isnull(@value,''))='') I want to know whether the above piece of code works in checking if the variable is null or empty.
Bharath
  • 573
  • 1
  • 7
  • 20
24
votes
1 answer

How to Groovy-ify a null check?

Is there a more "Groovy" way to write this Groovy code: def myVar=(System.getProperty("props") == null)? null : System.getProperty("props") Logic is: If System.getProperty("props") is NULL, I want props to be NULL; Else, I want props to be the…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
20
votes
1 answer

How to check a value in a SQLite column is NULL or not with C API?

I'm using SQLite with C API. On C API, I can check the result value of a column with sqlite3_column_* functions. the problem is there is no function for the case of the value is NULL. Of course, I can check the value with sqlite3_column_bytes…
eonil
  • 83,476
  • 81
  • 317
  • 516
18
votes
3 answers

C#, multiple == operator overloads without ambiguous null check

Introduction: I have a few classes which do the same work, but with different value types (e.g. Vectors of floats or integers). Now I want to be able to check for equality, this equality should also work in between the types (such as vectorF ==…
Chillersanim
  • 455
  • 3
  • 13
16
votes
2 answers

FindBugs detecter for NonNull Lombok builder attributes

I have a lot of classes with @NonNull fields using Lombok builders. @Builder class SomeObject { @NonNull String mandatoryField1; @NonNull String mandatoryField2; Integer optionalField; ... } However, this gives the caller the option…
John Bupit
  • 10,406
  • 8
  • 39
  • 75
15
votes
4 answers

javax.annotation.Nonnull vs assert

I'm using Findbugs and javax.annotation.Nonnull on method parameters. On private methods I usually add an assert line to check for nullness like private void myMethod(@Nonnull String str) { assert str != null .... Latest Netbeans version…
Gualtiero Testa
  • 244
  • 1
  • 3
  • 8
1
2 3
14 15