Questions tagged [null]

Null is the absence of a value. Null is typically used to indicate that a reference or pointer variable points to no object in memory.

Filter by
Sorted by
Tagged with
0
votes
1answer
20 views

How to handle adding new field / partial data update for a gRPC API?

Assuming we have following message that will be used to update data and it just got updated to version 2. message HelloRequest { string name = 1; // version 1 bool is_valid = 2; // version 2 } ...
0
votes
0answers
137 views

Should I check for nulls, even though I won't get any, as far as I know? [duplicate]

I'm new to software engineering and right now I'm focused on learning the best practices to consistently write robust code. Recently I've been maintaining an application built by other people and/or ...
0
votes
3answers
112 views

When to use a reserved word, null or undefined as a key in an object?

I have a case where I have a dictionary object and a value with no key. The object can have the system values and then a user value. I have to store that value. I could use a reserved word or I ...
38
votes
8answers
8k views

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

For example, suppose I have a class, Member, which has a lastChangePasswordTime: class Member{ . . . constructor(){ this.lastChangePasswordTime=null, } } whose lastChangePasswordTime ...
17
votes
2answers
612 views

C# 8 non-nullable references and the Try pattern

There is a pattern in C# classes exemplified by Dictionary.TryGetValue and int.TryParse: a method that returns a boolean indicating success of an operation and an out parameter containing the actual ...
-2
votes
1answer
101 views

Using static member methods to check for object being NULL

Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse. Something like, #include <...
0
votes
0answers
33 views

How to release collectiable assembly (cannot null belonging type)?

The documentation states You must release all objects that represent parts of the assembly. The ModuleBuilder that defines T keeps a reference to the TypeBuilder, and the AssemblyBuilder ...
2
votes
1answer
459 views

Is there a convention for the Optional/Maybe monad in typescript?

Let's take the classical example of a function that may return a number or not. In typescript this can be represented like this: function f(): number | undefined {} A more elaborate way would be to ...
-1
votes
2answers
161 views

Null checks good practice if code will only be ran when object is never null

Using a website with Javascript as example. Let's say I have script A which only performs a specific function on page Foo. For example something like sorting elements in a list. Script A is only ...
-3
votes
2answers
84 views

How to have zero or one of and object in a class? [closed]

I want a class A, and I want the relation that A can have zero or one X. So I wrote this class: public class A { private X x = null; public A() {} public A(X x) { this.x = x; } ...
0
votes
2answers
320 views

Is it beneficial to throw a caught IOException as an UncheckedIOException in order to prevent NullPointerException?

Not using UncheckedIOException, NullPointerException possible public void callerMethod() { Object result = ioMethod(); // call instance method of result } public Object ioMethod() { ...
1
vote
1answer
88 views

How to set nullable/optional property of database object to NULL in backend?

We are building API which mainly passes database objects back and forth between user and database, so the main flow of information is quite basic: Table (view) <-- ORM --> C#/Java/etc. Objects &...
3
votes
1answer
127 views

if null is bad how we justify the “rfc” nullable for php language?

I do not understand how you think about it:https://wiki.php.net/rfc/nullable_types when It is widely confirmed, that using nulls is bad practice Where am I wrong? thanks. I'm not criticizing !. I ...
0
votes
3answers
935 views

Setter with one parameter and null default value

Which solution is most logical? The value can be null, but when not null it must be a string. This (First): function setValue(string $value = null); To me this is bad; since we can now call the ...
25
votes
8answers
3k views

If nulls are evil, what should be used when a value can be meaningfully absent?

This is one of the rules that are beaing repeated over and over and that perplex me. Nulls are evil and should be avoided whenever possible. But, but - from my naivety, let me scream - sometimes a ...
1
vote
3answers
4k views

How to avoid != null statements? [duplicate]

I use code object != null to avoid NullPointerException. Is there a good alternative to solve this as follow ? if (someobject != null) { someobject.doCalc(); } This will not work for ...
3
votes
3answers
803 views

As API author, should I treat Empty and Null the same in search criteria?

I have a RESTFUL api, one of the endpoints is receiving search criteria which contains property for "Title". Should I allow consumers to send either null (or eliminate the property) or Empty string in ...
-1
votes
2answers
437 views

Should I throw ArgumentNullException and should I test it? [duplicate]

Let's take an example of class with 3 dependencies and one method. class Example { private readonly IDependency1 _d1; private readonly IDependency2 _d2; private readonly IDependency3 _d3;...
-1
votes
1answer
194 views

Null Object and Exceptions

Do Special Case or Null Object design patterns still provide value when application behavior, not just object behavior has to change? I was tasked with revisiting an old application and refactoring ...
8
votes
8answers
669 views

Is there a compelling reason why columns in SQL are nullable by default?

As a CS student, I've learned a decent number of programming languages over the years, most of which have had some concept of a "nullable" or "optional" type. Note that I'm not talking about null ...
2
votes
3answers
159 views

Is there anything wrong with making nullability of a reference type explicit by wrapping it in a value type?

When I program in Java, I make all nullability explicit; that is, an instance of Foo is assumed to be non-null, and if I want it to be null, I use a @Nullable annotation (or better, Optional<Foo>...
4
votes
3answers
409 views

Return NULL in case of success

This question is a clearer version of a question I posted on SO. I have a C++ Planner object with a method that computes a Route from a start point to a destination point. Planner is the owner of the ...
1
vote
2answers
166 views

Setting a pointer to NULL on failure?

I've been modifying some code written by a previous employee and came across a function with the following signature: BOOL WINAPI PrependPadding( _In_ SIZE_T cbPadding, _In_ SIZE_T cbRow, ...
3
votes
4answers
252 views

Advice for bugfixing object oriented code: why is data not set?

When debugging object oriented (Java) code, sometimes some field of some object is null while it should not be. I often spend what I feel is way too much time trying to figure out where it should have ...
0
votes
1answer
150 views

Setters and checking on null value

We are dealing with a lot of null values, now and we are wondering if there are any standards for avoiding it at some places in the code. For example I have this PHP code: class X { ...
1
vote
0answers
205 views

What programming language first used the 'Safe navigation operator' (?.)?

What programming language first used / invented the 'Safe navigation operator' (?.) ? Unfortunately, the Wikipedia page on this subject does not have information on it and googling yielded no readily ...
11
votes
1answer
361 views

What are some intuitions that support calling the Maybe constructor in Haskell “Just”?

The intuition of an optional type like Maybe Int is that either there is no Int (thus, there's Nothing there) or that there is some Int; there is something there. It makes sense to me that we call ...
1
vote
2answers
4k views

Unit tests for constructors

If I make a class, that in the constructor accepts an array or collection, but this array or collection is optional (the parameter can be null), and then I store that collection in the object in ...
2
votes
6answers
1k views

When do the benefits of nullable value types outweigh the cost of null pointers?

The question is in the title. Here is the context: Some people think that the null pointer is a big mistake. Tony Hoare famously apologized for inventing it. Since version 2.0 C# has had nullable ...
14
votes
4answers
4k views

Fetching a value without having to null check in Java

Many times I find myself null checking when fetching a value from some data hierarchy to avoid NullPointerExceptions, which I find to be prone to errors and a needs a lot of boilerplate. I've written ...
2
votes
1answer
2k views

Is it possible to make nonnull become part of C++ type system?

NULL is the billion-dollar mistake but there is nothing in the type system of C++ to prevent it. However, C++ already has const-correctness so implementing NULL-correctness seems trivial: introduce a ...
1
vote
3answers
726 views

What value does the byte pointed by null pointer have?

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL. However, the memory cell at that address does exists after all, right? In that case, ...
1
vote
2answers
153 views

Should I return true in a method that casts null from one object type to another?

I'm writing a very simple extension method that attempts to cast objects from one type to another. The intent of having this method is very similar to Int32.TryParse(string, out int), which allows the ...
1
vote
1answer
270 views

What native JavaScript functions/methods/properties/operators can return null? undefined? [closed]

I'm writing some code that needs to deal with many potential JavaScript values including so-called non-values . I'm realizing it would be very helpful to know if there are some general rules about ...
1
vote
3answers
436 views

Benefit of throwing exceptions for null parameters

The codebase I work with has a certain pattern prevalent in all public methods, which goes like this: public void UpdateUser(User userArg) { Framework.NullCheck(userArg); var user = userDb....
4
votes
1answer
5k views

What should I use for @Nullable if I used the lombok @NonNull?

I am using the lombok @NonNull annotation for some of my fields, parameters and methods, partly for null checking, and also partly for documentation purpose. It is then confusing if I added a @NonNull/...
2
votes
2answers
1k views

In Qt or C++, how should I check whether my `int` variable has been defined?

Short Problem: How should I check if integers are undefined, just as I can check QStrings for having NULL values? Backstory: This is my coding style when I am trying to avoid overloading my ...
0
votes
3answers
2k views

In C, why is NULL and 0 triggering an if statement

I have a function that is called by myStruct *structName = myFunction(0); The function looks like myStruct *myfunction(int x) { if ( x == NULL) { return NULL; } /*rest of ...
2
votes
3answers
2k views

A very basic question about whether I should check for null and throw NPE? [duplicate]

Consider the below method- public void operationOnList(List<String> list) { list.add(1); } It is obvious that if list is null this method will throw a NullPointerException. My question is ...
12
votes
5answers
5k views

Inheritance vs additional property with null value

For classes with optional fields, is it better to use inheritance or a nullable property? Consider this example: class Book { private String name; } class BookWithColor extends Book { private ...
2
votes
2answers
665 views

Law of Demeter and its applicability

Let's say I'd like to perform the following command: house.getFloor(0).getWall(WEST).getDoor().getDoorknob(); To avoid a NullPointerException, I'd have to do the following if: if (house != ...
1
vote
4answers
7k views

Should I check for nulls in setters and similar methods?

I have plenty of setters in my classes, as well as many methods to add an item or a set of items to lists or maps. Should I check for nulls for each one of them, or should I allow ...
71
votes
7answers
8k views

How does a surname of Null cause problems in many databases?

I read an article on BBC. One of the examples they said was that people with surname 'Null' are having problems with entering their details in some websites. No explanation is given about the error ...
5
votes
4answers
3k views

Is “use ”abc“.equals(myString) instead of myString.equals(”abc“) to avoid null pointer exception” already problematic in terms of business logic?

I heard numerous times that when comparing Strings in Java, to avoid null pointer exception, we should use "abc".equals(myString) instead of myString.equals("abc"), but my question is, is this idea ...
2
votes
5answers
11k views

Are there reasons to assign NULL instead of 0 to non-pointer variables?

Assigning variables with values during definition is a good practice. A common practice is to assign variables with 0 and pointers with NULL. int p = NULL; // instead of int p = 0; int *ptr ...
7
votes
2answers
3k views

Compile-time checking for NULL initialized std::string

This is sort of the complementary question to How to best protect from 0 passed to std::string parameters?. Basically, I'm trying to figure out whether there is a way to have the compiler warn me if a ...
2
votes
5answers
1k views

Handling null-references in C# logic

Let's say I have an API method with can be used to calculate the sum of all orders made by a specific customer: Amount CalculateOrderSum(int customerId) { // Perform authentication to make sure ...
5
votes
3answers
3k views

Is it good practice to have your C++/Qt functions always check all its arguments for null values?

Backstory While developing with Qt Signal/Slots, I came across a few segmentation faults that had me puzzled as to what was causing it. Eventually I figured out that you could actually pass a slot ...
9
votes
8answers
1k views

Designs and practices to guard against erroneous null entries from database

One part of my program fetches data from many tables and columns in my database for processing. Some of the columns might be null, but in the current processing context that is an error. This should "...
11
votes
4answers
32k views

Ruby: if variable vs if variable.nil?

I'm new to Ruby and I was surprised when I found out that all objects are true apart from nil and false. Even 0 is true. A nice thing about that property of the language is that you can write: if !...