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

Rails hash null check - which is better 'if-condition' and 'ternary operator'?

I have below hash instance_options[:param_page] instance_options[:param_page][:comment_page] instance_options[:param_page][:comment_per] I need comment_page, comment_per value I did refactoring code But I not sure what is best hash null check let…
Changwoo Rhee
  • 179
  • 4
  • 11
0
votes
0 answers

C# data table loop and find certain columns are not null

I have a datatable with required data. try { using (var connection = conn) { using (var command = conn.CreateCommand()) { command.CommandText = SqlCommand; …
GSR
  • 115
  • 2
  • 12
0
votes
1 answer

What is the most efficient way or best practice for null check when you use Split and FirstOrDefault methods together?

I use Split and LastOrDefault methods together, I use this code block for null check. But it does not seems the most efficient way to me. Because I use lots of check and && operators. It seem a little ugly. Is there any way to accomplish this…
cansu
  • 958
  • 1
  • 12
  • 23
0
votes
1 answer

check if null and do something

I'm new to Kotlin and I'm trying to figure out how to do something if a variable is null. This is what I'm trying to do private var loggedInUser: User? = null fun createActivity(activity: Activity) { loggedInUser?.activities?.put(activity.id,…
Maccaroni
  • 45
  • 2
  • 8
0
votes
0 answers

How can I compare two text files line by line and EXTRACT BOTH lines which differ to another file?

In bug prediction research topic, you need to compare version (N) from your code (Buggy code) with version (N+1) i.e (Fixed code) in order to get exact code changes and more over to normalise and anonymise these code changes to get bug…
Y-K
  • 1
  • 1
0
votes
1 answer

Can IntelliJ auto-generate code to null-check arguments when using @ParametersAreNonnullByDefault annotation on package

I like the feature where IntelliJ can automatically generate code to check at runtime for null on each argument/parameter passed to a method. This feature is enabled in Preferences > Build, Execution, Deployment > Compiler > Add runtime assertions…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
votes
2 answers

Null check + dereference on the same line

Are there any dangers in checking for null and dereferencing on the same line? If myObj is in fact null, how would this code behave? Are there differences in how different languages handle situations like these (ie C# vs Java) For example, something…
user8360241
0
votes
3 answers

How to check Boolean for null?

I want to add a null check to my ternary operator which checks on a Boolean isValue: public String getValue() { return isValue ? "T" : "F"; } My task is: What if the Boolean(object) return null? Add a boolean check and return "" (empty String…
learn n rock
  • 59
  • 1
  • 5
0
votes
1 answer

How to change default message generated by Resharper for empty string check (null-check)

Resharper has great feature of adding null check code for arguments. For strings it can add string.IsNullOrWhiteSpace check with ArgumentException. Everything's fine, but I need to have Resharper generate another message, and not Value cannot be…
0
votes
1 answer

Why does null check fail even though object is explicitly initialized as null

I have created a custom abstract class, which in turn of course derived classes were also created. public abstract class AbstractBaseClass ... public class ChildClass1 : AbstractBaseClass ... Now, whenever I declare for example AbstractBaseClass…
Michael Balser
  • 157
  • 1
  • 10
0
votes
1 answer

Missing Dart null checking shorthand?

I know that Dart has the null checking shorthands x = y ?? z; and x = y?.z; But what if I have a statement like x = (y != null) ? SomeClass(y) : z; I don't want to pass null to the SomeClass constructor - so in that case I want x to be set to z…
Magnus
  • 17,157
  • 19
  • 104
  • 189
0
votes
0 answers

How should the specification pattern handle null candidates?

I am just wondering if there is a recommended strategy for dealing with null candidates in the specification pattern. As I see it, there are two possible strategies: Throw an exception if the candidate is null. Return false for cases where null is…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
0
votes
2 answers

A better way to write null reference with return value in c#

Is there a better way to write this null check? I'm checking a table from a DataSet for nulls. if (dataSet == null || dataSet.Tables == null || dataSet.Tables[0].Rows == null) { Console.WriteLine($"Error at {nameof(dataSet)}"); return…
Paul_87
  • 87
  • 7
0
votes
1 answer

Validate that match can't be empty

Is it possible that javascript linter will tell if regex will allways produce nonempty match? s.match(/\w*/)[0] // valid s.match(/\w+/)[0] // invalid s.match(/id(\d{7})/)[1] // invalid s.match(/id(\d{7})|$/)[1] //…
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0
votes
0 answers

a.b.c.d type property access when undefined

// option 1 let val = a.b.c.d || null; // this gives error if a.b.c is undefined or a.b is undefined etc. // option 2: alternate version let val2 = null; if (a.b && a.b.c && a.b.c.d) { val2 = a.b.c.d; …