Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
0answers
17 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
139 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
80 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
120 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
84 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 &...
2
votes
1answer
109 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
369 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 ...
18
votes
7answers
1k 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
2k views

How to avoid != null statements [duplicate]

I use object != null a lot to avoid NullPointerException. Is there a good alternative to this ? As an example: if (someobject != null) { someobject.doCalc(); } This avoids a NullPointerException,...
3
votes
3answers
428 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
327 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
160 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
653 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
156 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
373 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
159 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
248 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
138 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
181 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
350 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
3k 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
925 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 ...
12
votes
4answers
3k 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
1k 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
592 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
119 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
259 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
271 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
4k 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
1k 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
1k 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 ...
13
votes
5answers
4k 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
586 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 != ...
0
votes
3answers
5k 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 ...
6
votes
4answers
2k 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
7k 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
2k 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 ...
4
votes
3answers
2k 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 "...
9
votes
4answers
24k 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 !...
2
votes
4answers
264 views

Alternative way to expose you're returning Nothing in a method

I'm kind of in an impasse here. Lets say I have module A which exposes some methods returning objets and module B that uses said methods from Module A. Module A will have situations when it will have ...
2
votes
1answer
301 views

Finding methods that return null instead of a collection (in Java)

I'm looking for a good tool or technique to search a Java code base and find every method that says it returns a collection (List, Set, Map, etc.) but sometimes returns null. This would be a cool ...
2
votes
7answers
2k views

Throwing an exception when a method does not complete or implement a work around? [duplicate]

I have been studying this subject quite a bit, and I am unsure of what's the best way. I still have a lot to go trough (books, blogs, stack exchange, etc), but I simply can't find a consensus. ...
0
votes
0answers
82 views

Checking for null pointers or allowing a math library to segfault?

I've been going through the GNU GSL design document and came across this interesting line in the Test suites section: N.B. Don't bother to test for null pointers -- it's sufficient for the library ...
9
votes
6answers
3k views

How to implement a property on class A which refers to a property of a child object of class A

We have this code which, when simplified, looks like this: public class Room { public Client Client { get; set; } public long ClientId { get { return Client ==...
2
votes
3answers
295 views

Null values handling in big scale applications

Tl;dr: Should we return null and not know origins of the error or throw exceptions and handle them appropriately? A few years ago I found this article: http://stackify.com/golden-rule-programming/ ...
30
votes
7answers
6k views

Should I validate a method call's return value even if I know that the method can't return bad input?

I'm wondering if I should defend against a method call's return value by validating that they meet my expectations even if I know that the method I'm calling will meet such expectations. GIVEN User ...