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

How adding more restrictions to the TypeScript type system can solve some errors?

I had some issues building my TypeScript project because the type definitions of a library had errors. The solution was to add the strictNullChecks flag. That's really counter-intuitive to me. How does adding restrictions remove errors? Usually,…
Cl00e9ment
  • 773
  • 1
  • 13
  • 30
3
votes
2 answers

What is the difference between Optional.ofNullable() and `Optional.of()

First of all, I know the difference between the two methods. Optional.of : Used to ensure that there is no null, if null is entered, nullPointExcepction Optional.ofNullable : may or may not be null. Used to respond flexibly. So, if I add…
바보린
  • 159
  • 1
  • 13
3
votes
2 answers

Why do Kotlin null checks, when decompiled to Java declares some unused variables?

I did what follows with Kotlin 1.5.20 on JVM 11 (AdoptOpenJDK build 11.0.4+11) Usually in Kotlin I perform null checks favouring x?.let {} or x?.run {} over if(x != null) {}. I decompiled to Java these three approaches, in order to understand if my…
Luca Piccinelli
  • 379
  • 1
  • 17
3
votes
2 answers

Java: how to avoid NPE in ternary operator, in general how to perform null-checks elegantly?

Yesterday I had to write an ugly piece of code, in order to perform many null-checks on the fields of an object, in order to avoid NPE from ternary operator construct. The problematic piece of code: ResourceThresholds rt =…
gai-jin
  • 653
  • 2
  • 10
  • 24
3
votes
1 answer

Object pattern matching results in uninitialized variable

Pattern matching to find whether a value is not null was done using the is keyword with the respective expected type (included a cast if necessary). The pattern !(obj is object instance) returns true if obj is not null and false if obj is null. At…
Crown
  • 169
  • 1
  • 7
3
votes
1 answer

Best/Fastest way to NULL check multiple file pointers?

I was wondering what would be the best and/or fastest way to NULL check multiple file pointers and rule out the 'bad' (NULL) ones? Can this be achieved via the switch statement? My 'normal/basic' approach would be: FILE *fp1, *fp2, *fp3; fp1 =…
annoyingnoob
  • 63
  • 1
  • 5
3
votes
0 answers

How is VS/TypeScript telling me Object is possibly null?

I've got the following TypeScript snippet: const anchorRef = React.useRef(null); ... const handleClose = (event: any): void => { if (anchorRef != null && anchorRef.current != null && anchorRef.current.contains(event.target)) { …
DontThinkJustGo
  • 380
  • 2
  • 11
3
votes
3 answers

Java 8 nested null check for a string in a map in a list

I need to do a series of null checks ( nested null-checks ) to get an array of strings like below String[] test; if(CollectionUtils.isNotEmpty(checkList)){ if(MapUtils.isNotEmpty(checkList.get(0))){ …
adbdkb
  • 1,897
  • 6
  • 37
  • 66
3
votes
2 answers

JS object null checking - weird JS problem

Imagine this simple scenario. I have variable that can be plain JS object with one property, ID, that is a number or obj variable can be null. I have simple test() function that checks if the variable is not null and that it must have valid id…
Tjodalv
  • 340
  • 3
  • 15
3
votes
4 answers

Use Java 8 Optional in existing Java 7 code

I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List loadMatching(Region region, String nameStartsWith,…
qrius
  • 621
  • 2
  • 9
  • 22
3
votes
1 answer

Check if yield return contains items

I'm trying to optimize a routine that looks sort of like this (simplified): public async Task> GetBars(ObjectId id){ var output = new Collection(); var page = 1; var hasMore = true; while(hasMore) { …
Asser
  • 109
  • 1
  • 9
3
votes
1 answer

NullReferenceException thrown by MVC View Model

I'm trying to sort out this issue but as I'm learning a lot of this stuff as I go along I'd really appreciate it if someone could explain where I'm going wrong and/or some good resources where I can read up. So, I have a model based on my Entity…
3
votes
3 answers

Performance hit of checking for null

Can anyone tell me what the performance cost is of checking if an object or property of an object is null in c#? I am working on an ASP.NET MVC application that the null checking is being done in the Model and then done again in the view. I feel…
Jeremy
  • 364
  • 3
  • 18
2
votes
1 answer

CA1062 (Code Analysis) disagrees with ReSharper -- Who wins?

protected override void OnTextInput(TextCompositionEventArgs e) { e.Handled = true; DoSomething(e.Text); } If I check for null, CA is happy, but ReSharper says that the null check will always be false. I'm not sure who is more trust-worthy…
myermian
  • 31,823
  • 24
  • 123
  • 215
2
votes
1 answer

Null-check behaves incorrectly when iterating linked-list in C

I am trying to learn C basics by writing a program which prompts for integers from user then stores the values inside a linked list. If an input is -128 or below, the current node is set to NULL and the prompt ends. The program then goes through the…