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

Making Handlebars.js exclude rendering attributes where the source value is null?

I'm trying to create dynamic XMLs based on data stored in a javascript object. The problem is that the source data for certain attributes may contain null, which will result in Handlebars rendering the attribute with an empty string, while I would…
robertpaulsen
  • 209
  • 4
  • 9
0
votes
1 answer

Error in If else statement for NULL check

Can anyone please help me to solve the error. I want to check in IF statement that, if there is no Null then insert the value. while (i < 33 ) { library(jsonlite) library(httr) get_reply2 <- my_get(staypoints$Stayplon[i],staypoints$staypLat[i],…
Saara
  • 105
  • 12
0
votes
1 answer

Java Null check elimination can cause bad code?

Recently, I read here about JVM optimizations , which are great, but i have a problem with one optimization, namely Null Check Elimination (or Uncommon trap) To sum up Null Check Elimination removes a if (obj == null) ... and hopes for the best,…
Tomer W
  • 3,395
  • 2
  • 29
  • 44
0
votes
1 answer

VB equiv - Elegant way to check if a property's property is null

I noticed this great post on how to do inline null checks on nested objects/properties: C# elegant way to check if a property's property is null Is there a VB.NET equivalent available?
J. Halbert
  • 11
  • 3
0
votes
1 answer

Is there any alternative to org.apache.commons.collections.CollectionUtils.isEmpty()?

It seems that org.apache.commons.collections.CollectionUtils doesn't contain isEmpty() anymore. Is there any other "common" library, which contains the same functionality?
LazR
  • 631
  • 8
  • 18
0
votes
1 answer

Is using 'or' for null check a good PHP programming practice?

When browsing the source code of a project I am contributing to, I find this line of code: $id or $id = $this->id; It seems to be working normally, and it is kinda like a workaround for the lack of null coalesce operator/handler in PHP 5. the 'or'…
Lord Yggdrasill
  • 3,197
  • 4
  • 26
  • 42
0
votes
1 answer

Spring MVC model - check parameter values in functions?

OK, so when you are working on some Spring MVC project and you've got some model that has a few setter functions like setCountry(String country) I know most of the code and books just set the parameter without any validation, but would it be good…
0
votes
1 answer

checking an array of string objects for null values

This is a fairly basic question I think but either my brain hasn't woken up yet or I'm just being thick! I have a class that has a property of a collection of strings defined below (names are simplified) public class IdentifierCollection :…
dreadeddev
  • 268
  • 5
  • 16
0
votes
0 answers

Restructuring data into nested maps - override put to avoid null?

I need to dump a list of somewhat complicated Java objects in YAML. To get the needed structure I first converted them into nested lists and maps. So it's kind of like: - item 1 name : value name : value - another list ... -…
0
votes
1 answer

How to resolve a null reference exception in a null check statement?

I've added a null property check to a boolean method, to prevent returning true, if any string fields are empty in a SelectedCustomer property. The problem is my bool method gets called from the constructor, before I've input any data into the…
Brian Var
  • 6,029
  • 25
  • 114
  • 212
0
votes
4 answers

How to stop program crashing when user does not enter a value?

So basically I have a problem. When the user presses the calculate button (I'm making a calculator) without entering a value the app crashes. I want it to display a message instead but I don't know how. (updated) @Override protected void…
Ashraf96
  • 53
  • 1
  • 3
  • 17
0
votes
0 answers

Cast to interface and check for null vs checking with "is" right away: What is best and why?

There are two alternatives I am considering as guard clauses to only allow some code to run if I have an object of proper type: var targetObject = eventArgs.Value as MagicType; if (targetObject != null) { DoStuffWith(targetObject); } or…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
0
votes
1 answer

How to loops over a dataset's table's DataRowCollection without null checking

I'm working with a dataset that is already populated. I am iterating over the rows in a table like so: foreach (DataRow row in this.dataSet.Tables["tablename"].Rows) { // do something } this works fine, unless there are no rows, in which case…
mattumotu
  • 1,436
  • 2
  • 14
  • 35
0
votes
1 answer

What's a more elegant way to check for null value in the following scenario?

I'm using Groovy. What's a more elegant way to do the following? if (!token) { token = this.getToken(key, callback) if(!token) { return params = null } } So if a token is already present when a user comes into this flow, that…
Paul Erdos
  • 1,355
  • 2
  • 23
  • 47
0
votes
2 answers

MySQL: null check based on if statement

I have this procedure: DELIMITER // CREATE PROCEDURE add_package(nam VARCHAR(255), pack_id INT, lib_id INT, descrip TEXT) BEGIN DECLARE ind INT; DECLARE `_rollback` BOOL DEFAULT 0; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` =…