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
1
vote
3 answers

How can I guard against "Object cannot be cast from DBNull to other types"?

I've got this code, where ISTM I'm defensively coding against assigning nulls: foreach (DataRow priceAndUsageVarianceRow in _dtUsage.Rows) { //var pauv = new PriceAndUsageVariance //{ // Description =…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
1
vote
1 answer

h:selectOneRadio is passing null character instead of null value

In my JSF pagecode, I have something similar to the following:
Chatoyancy
  • 143
  • 1
  • 17
1
vote
3 answers

Generic code to test if an Object is null?

Can someone help me provide a generic optimized code to test for null objects? What I am basically looking for is a method, that can take any object as input and maybe the path and return a boolean specifying if the target object is null or…
M Ah
  • 11
  • 1
  • 2
1
vote
0 answers

What is the best way to check null in Java

When we need check whether object is null we use x == null; but i saw in few places use null == x rather than x == null. so what is different between ( x == null ) and (null == x) why is the main reason to do this. is this effect to…
Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
1
vote
3 answers

Javascript null checking not working as expected

I'm using AngularJS as my framework and also connect to a webservice to fetch some JSON data. I have the following code: $login.onlineLogin($scope.credentials). success(function (data, status, headers, config) { if (data !== null ) { …
user818700
1
vote
2 answers

Null Checks for a large hierarchical object in Javascript

We have a large hierarchical object (worst possible legacy design) in javascript. The problem I am facing is that there is a list of null checks I need to perform whenever I want to access an element deep within the object structure. Say I have a…
DntFrgtDSemiCln
  • 1,259
  • 2
  • 16
  • 35
1
vote
5 answers

Java null check on object

Okay, its a simple question, I dont have any problem with it but I just want to be sure. Is it correct if I do something like this ? if (person != null && person.getAge > 20) {...} can I do this? null check and object access on the same line? is…
1
vote
6 answers

Java: What is the best approach to validate the method arguments for null

What is the best approach to validate the arguments for null while calling a method? 1) before making the method call private void myMethod(String param1, String param2){ String a = param2; if(StringUtils.isNotBlank(a)){ validate(a); …
ams2705
  • 287
  • 2
  • 5
  • 17
1
vote
5 answers

calling function should return default value, if object (or any function result) is null

Is it possible to wrap following code in a reusable function? EDIT: this is just an example, I want a working solution for ALL recursion depths what I want is that following code is generated: if (MyObject o == null || o.getSubObject() == null…
prom85
  • 16,896
  • 17
  • 122
  • 242
1
vote
2 answers

Avoid Null Checking by using lambdas

In this article Avoid Null Checks by replacing finders with tellers the author gives a Ruby example to avoid null checking, if the object is returned the block is run, if not then it isn't. data_source.person(id) do |person| person.phone_number =…
Michael
  • 1,849
  • 18
  • 38
1
vote
2 answers

Can default checks against null be done in a better way?

Quite often I find myself in need of writing stuff similar to: _parsedBetData["prizeLevel"] = params["prizeLevel"] == null ? "default" : params["prizeLevel"]; I am curious if there is a better way to do this? My main concern is that I have to…
Daniel MesSer
  • 1,191
  • 1
  • 7
  • 13
0
votes
1 answer

if-statement null-check not working in Kotlin, why?

As shown in below, in debug, if-statement with null-check is entered even though the variable "result" is null. enter image description here Blue line is the line next to be executed. So if-has been entered. This is how "result" is initialized: var…
furco
  • 3
  • 2
0
votes
4 answers

Why I can't check if `response.body` is null or not?

I am trying to see if a username already exist in the database or not, using the following code: Future checkUsername(userName) async { try { final response = await http.post( Uri.parse('http://10.0.2.2:3000/username'), …
Hasani
  • 3,543
  • 14
  • 65
  • 125
0
votes
1 answer

Do I have to null check every time before referencing an element in view binding?

So this isn't really a issue, I am just curious, do I really have to null check every time I use view binding? I find it a bit annoying having to put the question mark every time (binding?.[layout item]). Can somebody please give me a solution to…
0
votes
0 answers

In Visual Studio 2022, how can I change the null check template?

I frequently apply null checks to my parameters when developing in C# (like any good developer should!). Because I can be lazy, I'd prefer to let Visual Studio add these for me, which it can. However, when it adds the null checks, it consumes 4…
Brian Kessler
  • 2,187
  • 6
  • 28
  • 58