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
6
votes
1 answer

1 linear, beauty and clean way to assign value if null in C#?

Before you rush to think about the ?? null coalescing operator: string result = myParent.objProperty.strProperty ?? "default string value if strObjProperty is null"; The problem here is when whether myParent or objProperty are null, then it will…
renzol
  • 183
  • 2
  • 8
6
votes
5 answers

How to write a generic isEmpty method which can check for null, empty?

I am writing a utility method which can check for empty and null string, or collection or an object or any general types - public static boolean isEmpty(Object obj) { if (obj == null) return true; if (obj instanceof Collection) …
john
  • 11,311
  • 40
  • 131
  • 251
6
votes
2 answers

Why does String.Equals(Object obj) check to see if this == null?

Possible Duplicate: Why check this != null? // Determines whether two strings match. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public override bool Equals(Object obj) { //this is necessary to guard against…
myermian
  • 31,823
  • 24
  • 123
  • 215
5
votes
3 answers

Check if Array element is not null in one line C#

I got a neighbor array (consisting of Tile objects), that always has the length of 4, regardless if all elements are filled or not. I want to scan through that array and change the color of a PB contained in the Tile if that element / position is…
Sander Koldenhof
  • 1,223
  • 4
  • 13
  • 26
5
votes
2 answers

C# Repetitive code with null check

I'm working with RPC(protobuf-remote) and I need to do some checking in case the other end(server) is down. Let's say I've lot's of RPC methods, like: public FirstObj First(string one, string two) { if (rpc == null) return…
user6168313
5
votes
5 answers

Struts2, String null check

I'm trying to do a null check on a String but it won't work. 0
user425367
5
votes
1 answer

Null analysis in eclipse compatibility break between 7 and 8

I have encountered strange behaviour of nullcheck analysis under Spring tool suite 3.6.2 (Eclipse clon) on windows 7 64bit with Oracle java 8u25. The same maven project with java 7 source compatibility successfully finds NPE error in eclipse, but…
kulatamicuda
  • 1,603
  • 2
  • 21
  • 40
4
votes
1 answer

Efficient null check in Java 11

I need to use a getter which is 3 levels down in the object. response.getServiceError().getErrorCode() It is possible one or more objects could be NULL. Now, I am doing this if (response != null && response.getServiceError() != null &&…
Trozan
  • 89
  • 2
  • 11
4
votes
1 answer

Java 8 requireNonNull method with supplier parameter performance

Method Objects.requireNonNull with Supplier added in Java 8, but I'm not sure what's the performance improvement declared: While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
4
votes
2 answers

kotlin inverse boolean safe casting

Let's say I have an object Response. Now I would like to check a boolean variable, success, under Response and do an early return is response is not successful. if(response == null || !response.success){ return; } //Java version Now I would…
Yao
  • 709
  • 2
  • 11
  • 22
4
votes
1 answer

Does C# JIT compiler optimize null-check?

There are many articles online that list the optimizations made by the C# JIT before executing a piece of code. For example this post on MSDN talks about: Constant folding, Constant and copy propagation, Common subexpression elimination, Code…
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
4
votes
1 answer

Java 8 avoiding null pointer checks using Optional

Is it possible to write something like this and to avoid checking if elements are not null and collections not empty: response.getBody() .getRequestInformation() .getRequestParameters().get(0) .getProductInstances().get(0) …
Aleksandar
  • 119
  • 2
  • 13
4
votes
1 answer

how to do null \ nil check in LUA with redis-scripto and redis DB?

I'm writing a script in node.js, using scripto, and I'm trying to do a nil check to a value from the database: here is the js code (for node)- var redis = require("redis"); var redisClient = redis.createClient("6379","localhost"); var Scripto =…
Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
4
votes
4 answers

Java string null check by != null or !str.equals(null)?

What's the best way to check for not null values in java. String query ="abcd"; query != null vs !query.equals(null). which is better?why?
yonikawa
  • 581
  • 1
  • 9
  • 32
3
votes
2 answers

How to disable "unnecessary test for null" warning in NetBeans 14?

Short Version How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE? Long Version NetBeans as a well-known bug 1 2 3 4 where it will erroneously tell you that a test for null is unnecessary. For example in the following…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1 2
3
14 15