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
1 answer

Behavior of a controller action on reception of a null dto

When a controller detects that a DTO passed to it is null should it instantiate an instance of the DTO (cf the Null Object Pattern), or should it simply throw an exception there and then? Thanks
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
0
votes
2 answers

How to handle optional belongs_to association

USECASE: Consider the following example. Class Foo < ActiveRecord::Base belongs_to :user attr_accessible :title end Class User < ActiveRecord::Base has_many :foo attr_accessible :name end If a logged-in user creates Foo, it will be associated…
manoj
  • 1,655
  • 15
  • 19
0
votes
3 answers

OOP Initializing instance variables with instances of child class

I am trying to implement the NullObject design pattern on my class Node: class Node{ Node nextNode; char key; Node prevNode; /* Would like to initialize nextNode and prevNode to instance of …
Sush
  • 25
  • 4
0
votes
1 answer

undefined method for nil:NilClass refactoring?

in transactions, sometimes customers do not have service_id because they did not add additional services to cart. problem is, transactions index page returns undefined method 'name' for nil:NilClass on line <%= transaction.service.name %> Is there…
Hadi Farnoud
  • 131
  • 1
  • 15
0
votes
2 answers

Public ActionResult Delete(int id, brand brand) - Null brand object (MVC3 & Entity Framework)

I am learning MVC 3 with Entity Framework and I have a problem with my delete action result method in my controller. I have two ActionResult methods for delete, one with [HttpPost] for submitting data to the database another for populating the view…
nick gowdy
  • 6,191
  • 25
  • 88
  • 157
0
votes
4 answers

Checking invalid state when using Null Object pattern

When using Null Object pattern, how would you 'check' if a situation is invalid? for e.g when there's no item found in the repository, display message 'not found' to the user. Do I have to do a glorified null check? if obj.equals(new…
ronaldwidha
  • 1,345
  • 2
  • 12
  • 24
-1
votes
1 answer

How to solve null references error? Im new at this

It says that my getOutletId() is null references and i already declared, i think. So basically this is a booking step 4. For the first step, user need to choose outlet from firestore and choose which branch and i've already include to setOutlet id.…
-1
votes
1 answer

Delphi10 ObjectPascal: how do I just get to access the fields in a ADODataSet single record

So really happy with connecting to the DB and all that. My Grid object is fine and showing all records etc. Really happy with SQL. And the concept "DBs are all about sets" My issue is: I create an ADO query or dataset object where the SQL returns…
-1
votes
1 answer

c# what object literal could stop a foreach loop from looping?

I know I could wrap the entire foreach loop in an if statement, but I'm wondering if there's some object literal value replacement for new List() so that the foreach skips executing when myList is null? In Javascript I could simply put []…
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
-1
votes
2 answers

"Cannot convert from 'A' to 'B&'

I'm building an Entity-Component system using template metaprogramming. I keep getting either Cannot convert from [base type] to [type user requested]& or Cannot convert NullComponent to [type user requested]& errors: class Entity { public: …
Casey
  • 10,297
  • 11
  • 59
  • 88
1 2 3 4 5
6