Questions tagged [null-object-pattern]

A pattern where a specific object is used to represent a null, rather than a true programmatic null, in order to avoid various complications that arise from using an actual null. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

Certain protocols and adapters can explode when processing an actual null. To avoid this issue, and to make code simpler (dispense with all null checks on the client side), a special "token" object that represents a "null" is used in place of a null.

It was first documented in Bobby Woolf in "Pattern Languages of Program Design 3".

See Wikipedia for more.

85 questions
0
votes
0 answers

C# Null Object design pattern behavior in EntityFramework models implementation

I'm updating the framework of a project from asp.net core 3.1 to .NET 6 following the documentation on microsoft learn. Everything seems transparent enough, so I decided to refactor the code by enabling the null object design pattern. I noticed a…
0
votes
0 answers

Not returning a null list but also not returning an immutable empty one and alternative approaches

I have a method that returns a List. E.g. List getItems(Bar bar, X x) I don't want the method to return null to avoid null pointer exception and do something like: List getItems(Bar bar, X x) { if(!bar.conditionA() || !x.conditionB())…
Jim
  • 3,845
  • 3
  • 22
  • 47
0
votes
2 answers

null pattern and composite pattern don't play nice together

In this composite tree I am keeping a reference to the parent node for flexible tree traversal. I don't want to have to check the parent for a null reference all the time but if I make a NullNode class and initialize each node's parent to this I…
0
votes
0 answers

starting another activity (recyclerview list) by clicking on item

My main activity (MainActivity) contains Recycler view with list of images and text in cardview. I am trying to put separate onCLick listener on each cardview so that when any card view is clicked, the next activity starts(MainActivity2). the…
0
votes
2 answers

system.text.json explicitly set null attributes while ignoring unset attributes when serializing

I am trying to serializing a POCO object into an update request. My unset values are serialized as null, which causes them to be updated to null. If I set IgnoreNullValues to true this fixes the problem. However, then there is no way to explicitly…
0
votes
2 answers

Null Object Pattern on _base_ values

Let's say we have following DTO object which is representation of a record in a database: public class UserDto { public int Id { get; set; } public DateTime? ExpireOn { get; set; } } So Id property is not nullable and ExpireOn is. I have a…
Rok
  • 451
  • 3
  • 21
0
votes
1 answer

how do i slightly change the postion of an object in after effects that is connected to a null object?

I am trying to only slightly change the poison of an object that is whipped to a null thisComp.add(125,250).layer("Null 1").transform.position and it wouldn't work. right now a null is making sure that the object is moving forward but it is moving…
0
votes
1 answer

Javascript checking for null objects and caching values

In javascript re-introduction, I went through 2 examples that I have no clue when or where to use them. Below a quote: The && and || operators use short-circuit logic, which means whether they will execute their second operand is dependent on the…
0
votes
3 answers

Interesting thought problem on refactoring code that returns a null

I am interested in hearing your feedback. I've recently seen some Java code that is implemented in the following way: Object1 SomeMethod(String key) { Object1 object1 = null; List objectList =…
TERACytE
  • 7,553
  • 13
  • 75
  • 111
0
votes
1 answer

Empty interface for Null Object pattern

I have some DTO object for transfer data with WCF. public class Foo { //Many fields } WCF service method returns this object, and I have the valid case when this object should be null. I want to use the null object pattern to return something…
nuclear sweet
  • 1,079
  • 10
  • 27
0
votes
0 answers

Attempt to invoke virtual method...on a null object reference problem

I have a problem with my code, where sometimes I get a null object reference and sometimes not (on the same thing) and I cant understand whats the problem because as I said, one time its working fine but on the second time it shows me the null…
JTT511
  • 23
  • 6
0
votes
0 answers

null object pattern unable to solve the problem of multiple return types

Sometimes users prefer not to disclose marital status, which is fine in my domain. So I have data where user is either married or not and I have data where I don't know the status. My goal is to make sure the function for marital status check as…
Waku-2
  • 1,136
  • 2
  • 13
  • 26
0
votes
2 answers

Template NullObject: Is it a sin?

I coded this little Template-NullObject in Java and wanted to ask you wether it would be considered a sin to use one of these. When creating a NullObject, you typically make one which was specifically designed for one type of object, this one isn't…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
0
votes
2 answers

How to use Guava Optionals in a non-generic context?

I'm trying to use the Null-Objects from guava in the following method: private void display(Optional message) { ... } The method in which I am calling the method display(..) looks like this: if(...) { display(Optional.of("hello"); }…
Mirco Widmer
  • 2,139
  • 1
  • 20
  • 44
0
votes
1 answer

Null Object Pattern in a doubly linked list

I'm trying to use the null object pattern in a doubly linked list in c++, but I can't seem to find a way to use it while keeping the code clean. The problem lies in the following piece of…