Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [null]

This tag should be used for questions where you are handling null variables or values.

Filter by
Sorted by
Tagged with
4
votes
2answers
82 views

handle null in Comparator class

I have a class LoanAccount that contains an attributes creationDate and loanAmount. I ...
2
votes
3answers
1k views

String whitespaces

Here is a shortest possible syntax for replacing whitespaces with null and throwing an exception where it is not allowed: ...
5
votes
2answers
87 views

Multiple null Checks or try/catch NullPointerException [closed]

There is A LOT of information online stating that you should NEVER catch a NullPointerException. Generally I agree, but I am wondering about this one case. I have inherited code that requires me to ...
6
votes
4answers
526 views

Handling Null values (and equivalents) routinely in Python

I've found the following code invaluable in helping me 'handle' None values including "whitespace" characters that should be treated as None based on the situation. I have been using this code for ...
4
votes
4answers
195 views

Reading a text file that may have embedded nulls

I've always used fgets to read a file. However, I want to read a file that may have embedded \0. I thought of using ...
0
votes
1answer
67 views

Checking For multiple null references [closed]

So this question might be a bit opinion based but I am a bit confused, say i have these two code snippets - ...
2
votes
1answer
49 views

TypeScript 3 form validation object

Please, correct me with anything that I say in here (The actual question is below the code). I've been quickly prototyping a project (ASP.NET Core 2.1) and haven't found the need to structure it with ...
-2
votes
1answer
54 views

Refactoring null check before adding to Func collection with different predicates [closed]

I'd want to refactor that if ladder but I'm not sure how to do that or whether it is even possible ...
8
votes
1answer
498 views

Getting ready for Nullable Reference Types

Looking forward for C# Nullable Reference Types in C# 8.0. I do not like to use the preview implementation in a production code though, so just went with these simple helper classes intending them to ...
1
vote
1answer
138 views

Using coalescing and conditional operators to check API arguments

I have a web application that allows users to create customs items and later change those items. For eg. a change, the application takes the parameters and performs some basic sanity checks. If the ...
3
votes
3answers
86 views

Transforming XML with null checks vs variables

I'm working on an Spring Integration application that transforms a XML (converted by Jaxb) into a custom Object by checking various information given in the XML. The problem is that I can't trust ...
3
votes
2answers
364 views

Kotlin extension functions with null checks

Kotlin defines extension functions like also but they are not null checked. That's fine because you might not need the check. In case you do though, do you see any ...
2
votes
1answer
67 views

Comparing JSON documents for changes to values

Please go through below code and improve the quality by reducing the IF conditions: I have 2 jsons, first one is the source and second one is the updated json. I have to compare both values and set ...
6
votes
2answers
1k views

Smart pointer that cannot be nullptr

Sometimes I need a reference counting smart pointer that should not be nullptr. For example, I want to share a very heavy object without copying. And it's more convenient to place it on the heap. So I ...
4
votes
3answers
200 views

Caching an integer - distinguishing no value from zero

I would like to store an integer value in the application cache. The GetOrSet method should always return an int, and never be null. If there is no cached value, ...
2
votes
1answer
45 views

Method that concatenates strings, some of which may be null, with several different separators

I'd like to refactor a Java method I've created, making it cleaner and more beautiful. The method concatenates some strings, which may be empty or not. I'm new on java so i'm not sure how to do that, ...
2
votes
2answers
95 views

Invalidate a JS Object when any of its values is null or empty

I created a simple Javascript method to recursively parse a JS Object and find out if any of its values or sub values is set to null or is empty string. I could't ...
1
vote
0answers
97 views

Linear search on array

I've recently used this block of code from a quiz app where I check an array for the correct answer that was received from the browser and sent to the mobile device/application. ...
1
vote
1answer
354 views

NullPointer Exception handling in the code [closed]

...
-1
votes
2answers
123 views

Verifying that all fields of a unit has been suitably filled in

I have unit class which have some properties, and I have acceptancePolicy table which contain values. If values (from one row) are true method should return true. The method is below. How can I ...
2
votes
1answer
302 views

Is catching null pointer exception to handle it instead of null check beforehand always a bad practice or it depends? [closed]

I wrote a static method for an enum that returns the corresponding enum instance based on the source string, trimming any whitespaces and ignoring case as: ...
2
votes
1answer
129 views

C# Function: IsAvailable<T>(T value)

I'm sick of writing negative code statements like String.IsNullOrEmpty(value) and so I'm writing a function that checks for availability. It should answer the ...
0
votes
1answer
1k views

Loop to check isNullOrEmpty then setVisibility & setText

My code below is working fine but I know it can be done more efficiently using a loop. I'm checking the VAL (such as description, ingredient2, etc.) which are Strings from my SQL lite Db to see if ...
4
votes
1answer
12k views

Removing key/value pairs in list of dicts

I have a list of dicts that all have the same keys. If a key's value is None in all dicts then I want to remove them (A solution that creates a new dict is fine as ...
3
votes
2answers
3k views

Generic extension method for NullIf<T>() supporting nullable and non-nullable value types

I would like a simple, modestly efficient NullIf() generic extension for nullable and non-nullable value types including enums. The trickiness seems to be with ...
1
vote
2answers
49 views

Parameter guards for Firebase calls

I have code to ensure parameters existence all over the place. Is there a better way to do it? So that I do not have so many repeated codes inside different functions ...
2
votes
1answer
141 views

check for null before or after casting

I have a list of BasicDBObject that I fetch from my database. This is basically a JSON document converted into a java Object. Let's assume that we have a ...
2
votes
1answer
99 views

Thumbnail caption in vue.js with possibly missing fields in caption

I am using the following to output a thumbnail with text. It works fine but I don't like having the use a conditional to check every prop before outputting. E.g. this.post.user.name would indicate "...
0
votes
1answer
32 views

Extracting book information from a web page, with possible missing attributes

I'm just starting to code and I found a solution to first check if a div class exist before to try to get_text (and avoid an error if it doesn't). But the code ...
3
votes
1answer
227 views

Redis lookup with a default value

I have some code to perform a redis lookup, but if the key to look up either doesn't exist or there's an issue connecting to Redis at all, it will use a default value. The logic currently looks like ...
2
votes
2answers
80 views

Selecting a thumbnail from among a large, medium, and small image

I'm parsing some data in C# where I have an object with 3 properties: "large", "medium", "small"; each property may contain a string array with an image path, and I'm trying to extract that path. The ...
1
vote
2answers
490 views

Should I use setters with null or no setter when null value?

I would like to know what are the drawbacks of setting values this way: ...
6
votes
3answers
2k views

REST API to list books, where null means “everything”

My company has a REST API, and one design issue that has come up a few times is that we use null to indicate "get everything". For example /api/books?genre=romance ...
6
votes
3answers
822 views

Checking whether textboxes have data

I always end up creating a quick return statement above a code block if the simple condition of textbox null fields are found. Naturally, these tend to build up the more controls are added. Now I'm ...
12
votes
6answers
93k views

Generic Null/Empty check for each property of a class

I have created a method to check for null/empty values of class properties and if any null property is found I'm stopping the checking process and returning the result as true. I've used a solution ...
4
votes
2answers
153 views

Setting fields, with default values, before saving a row

I would like to find out if there's a cleaner way to go about validating input before it goes into a function. What I'm doing seems like a lot of code, I'm sure there's a better way. I would also ...
6
votes
2answers
583 views

An attempt to make a linked list iterator “safe”

I was practicing my C++ routine and decided to code up this simple linked list data structure. Furthermore, I made an attempt to make the list iterator "safe" in the sense that it throws an exception ...
2
votes
4answers
11k views

Calling a function on an object if not null

Here is code for check null in object: ...
2
votes
2answers
2k views

Pythonic way to convert XML attribute to float, or else get None

My Python code is converting XML attributes in an ElementTree to appropriate values. How do I best convert a value in this dict to a ...
9
votes
4answers
16k views

Inserting a DBNull value into a database

I have code which works pretty well. I just need some of your opinions on how to write it better with fewer line of code. I want to use a ternary operator but I couldn't make it work so I did an ...
6
votes
1answer
128 views

Tomorrow: rainy with a chance of null objects

In an attempt to hide from the sun outside, I gave the community challenge "Rainfall" a try. Code here, specific questions below. ...
2
votes
2answers
86 views

C# enumerable property like .Any that accounts for nulls

Brief Description: A generic extension method that performs the same function as IEnumerable.Any(), but does not throw an error when passed a parameter with a <...
3
votes
1answer
444 views

Grabbing SQL records: Nullable bool to bool

So I have a method that does a lookup in an existing List<T>, populated elsewhere by grabbing the records from a SQL table holding configuration data for each ...
2
votes
2answers
703 views

Row-wise mean imputation in R

Someone on SO asked how to fill in the NA's with row means. His example code was: ...
1
vote
3answers
91 views

Instantiating a property when the required attributes have been set

I have a simple class which holds my product. Someone can add a serial to this product and also a purchase id. On change of the properties, I want to check whether both properties are set, and get the ...
1
vote
1answer
225 views

Validating that only certain values are unset

I am checking data output in some testing code, and only some fields can be None, but I think this can be better: ...
1
vote
1answer
863 views

Rails Grape API Endpoint optional params validation in update

I have the following rails grape api endpoint in the users resource: ...
2
votes
1answer
1k views

Avoiding “warning: Use of uninitialized value” when fetching an integer CGI parameter

I was using this code to fetch a CGI parameter: $page = int(param('page')); This sometimes results in: ...
3
votes
2answers
154 views

Custom map on `Array`

I have the following problem: Given an Array, apply a certain method x to each Array ...
2
votes
4answers
498 views

Throwing exceptions if there is not exactly one box retrieved

(Note: this is originally posted as this SO question ) I'm trying to refactor the error logging and exception throwing into a method. Original code: ...