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
14
votes
6 answers

Is there a ?. operator for Java to perform null pointer checking?

When I code I am frustrated with null checks. Especially while coding data structures! I have to do it as String val; if(a != null && a.child != null) val = a.child.getValue(); else val = null; Something sort of this... Is there a operator…
Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37
14
votes
4 answers

Null checking is ambiguous for a class with several overrides for == operator

I have a class with two overrides for == operator, to compare it to other instances of this class, and to compare to instance of string. class SomeClass { string value; public SomeClass (string _Value) { value = _Value; } …
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
13
votes
4 answers

Java 8 optional add return result only if optional.isPresent

I have a piece of code where an interface has an Optional return method and some of the classes that implement it to return something, other don't. In an effort to embrace this brilliant "null killer", here is what I have tried: public interface Gun…
Vale
  • 1,104
  • 1
  • 10
  • 29
12
votes
3 answers

Remainder operator on int causes java.util.Objects.requireNonNull?

I'm trying to get as much performance as possible from some internal method. The Java code is: List writers = Lists.newArrayList(); private final int taxos = 4; [...] @Override public int getParent(final int globalOrdinal)…
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
12
votes
4 answers

Is it necessary to check null values with constructor injection?

I'm using .NET Core constructor injection. In a code review from a colleague, he raised the question if I should check for null values on injected dependencies in controllers. Since the framework is responsible for creating an instance of the…
12
votes
2 answers

Automapper: Checking for null in MapFrom

Using version 4. How do I check for null when doing a map? I tried the .Value, but that's not there on a Null: Mapper.CreateMap() .ForMember(x => x.DateApproved, y => y.MapFrom(s => …
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
10
votes
4 answers

Add to List value on HashMap

I'm populating a hashmap to have my objects grouped by one of their attributes. I find it “ugly” to check whether the list (the value) exists before adding my object to it. An example will be more explicit: // Need a map to group Person by age. //…
Fundhor
  • 3,369
  • 1
  • 25
  • 47
8
votes
3 answers

Null checking extension method

So, I'm doing a lot of database work in an application - and there are several possible return values of my caching system. It can return null, it can return a default (type) or it can return an invalid object (by invalid object, I mean one with…
caesay
  • 16,932
  • 15
  • 95
  • 160
8
votes
2 answers

Check if any object in get chain is null without having to check each depth

I'm working with a legacy application where I often have to access properties deeply nested like that: a.getB().getC().getD().getE().getF() The problem is that it's possible that in any depth, the value could be null. Of course I could check each…
Christian
  • 22,585
  • 9
  • 80
  • 106
8
votes
2 answers

Null coalescing operator in React JS/ Typescript

We have the Null coalescing operator in .NET and we can use as below string postal_code = address?.postal_code; Same thing can we do in React JS? What i found like we can do with && operator in address.ts file string postal_code = address &&…
Praveen Kumar
  • 1,966
  • 1
  • 9
  • 16
8
votes
8 answers

Java Object Null Check for method

I need to create a null check in this formula for the books[i] and I am not entirely sure how to go about this as I am not greatly familiar with null checks and very new at programming. Any and all help is much appreciated! public static double…
CoShark
  • 129
  • 2
  • 2
  • 12
7
votes
1 answer

!= null VS ?.let { } performance for immutable variables in kotlin

From what I understand, the big advantage of ?.let{} over != null is it guarantees that a mutable value is not changed inside the block. However, in case of an immutable variable is there a performance difference? For example, I have a simple method…
Feedbacker
  • 890
  • 6
  • 17
6
votes
4 answers

AnimatedSplashScreen PageTransitionType error

I have a problem using AnimatedSplashScreen, everything works fine to the moment I add a pageTransitionType. Then I get an error: The following _CastError was thrown building AnimatedBuilder(animation:…
JerZaw
  • 515
  • 1
  • 4
  • 9
6
votes
4 answers

How to avoid null checking before foreach IList

I have the following code: IList testList = null; ... if (testList != null) // <- how to get rid of this check? { foreach (var item in testList) { //Do stuff. } } Is there a way to avoid the if before the foreach? I saw a…
Milen Grigorov
  • 332
  • 3
  • 13
6
votes
4 answers

C# null check chain in method call

I suppose method call chain below. void DoSomething() { ObjectA a = CreateA(); if (a != null) { a.Foo(); } } ObjectA CreateA() { ObjectB b = CreateB(); if (b != null) { ObjectA a = b.ToA(); …
seung hwan Son
  • 183
  • 1
  • 7
1
2
3
14 15