Null object design pattern question

I recently watched this youtube tutorial on the Null Object design pattern. Even though there were some errors in it: such as the NullCar that doesn’t do anything creates an infinite loop, the concept was well explained. My question is, what do you do when the objects that can be null have getters, and are used in your code? How do you know which value to return by default? Or should I implement this pattern inside all the objects? What if I need to return strings or primitives? I’m talking from a Java perspective.

EDIT: won’t I be trading null objects testing for default value testing ? If not , why not ?


As far as I’ve understood it the idea is that the null object’s value is as close to “nothing” as possible. That unfortunately means you have to define it yourself. As an example I personally use “” when I can’t pass a null String, null object number for me is -1 (mostly because by default most database sequences start at 1 and we use those for item id:s a lot so -1 is dead giveaway it’s a null object), with lists/maps/sets it’s Collections.EMPTY_SET, EMPTY_MAP or EMPTY_LIST and so on and so forth. If I have custom class I have to create a null object from, I remove all actual data from it and see where that takes me and then apply what I just mentioned until it’s “empty”.

So you really don’t “know” which value to return by default, you just have to decide it by yourself.


How should I cast for Java generic with multiple bounds?

Is it possible to cast an object in Java to a combined generic type?

I have a method like:

public static <T extends Foo & Bar> void doSomething(T object) {
    //do stuff

Calling this method is no problem if I have a class that implements both interfaces (Foo & Bar).

The problem is when I need to call this method the object I need to pass to it is received as java.lang.Object and I need to cast it to make the compiler happy. But I can’t figure out how to make this cast.


The problem lies in a function like this:

public void problemFunction (Object o) {
  if ( o instanceof Foo && o instanceof Bar) {
      doSomething((Problematic cast) o);



Java 8 introduces the possibility of casting with additional bounds. You can cast an Object as a class with multiple interfaces (or just as multiple interfaces).

So this:

doSomething((Problematic cast) o);

simply becomes to this:

doSomething((Foo & Bar) o);

Why are there no byte or short literals in Java?

I can create a literal long by appending an L to the value; why can’t I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast?

And if the answer is “Because there was no short literal in C”, then why are there no short literals in C?

This doesn’t actually affect my life in any meaningful way; it’s easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it’s one of those things that bother you when you’re up late at night. Someone at some point made a design decision to make it possible to enter literals for some of the primitive types, but not for all of them. Why?


In C, int at least was meant to have the “natural” word size of the CPU and long was probably meant to be the “larger natural” word size (not sure in that last part, but it would also explain why int and long have the same size on x86).

Now, my guess is: for int and long, there’s a natural representation that fits exactly into the machine’s registers. On most CPUs however, the smaller types byte and short would have to be padded to an int anyway before being used. If that’s the case, you can as well have a cast.


How to resize a container div to the total height of its children?

I have a container element which I need to resize as its contents change. It contains 2 absolutely positioned divs which can both change height. If I don’t specify the height of the container then anything after the container disappears under the contents.

At the moment I am doing the following but I’d be pleased to find a less laborious alternative:

(container has position:relative, #main and #sidebar are position:absolute, the contents of #sidebar have no positioning specified)


div#mapcontainer { position:relative; width:100%; height: 600px;  }
div#main { position:absolute; top: 0; left: 10px; width: 500px; height: 400px; }
div#sidebar { position:absolute; top:10px; right:10px; width: 155px; height: 405px;}


<div id="container">
    <div id="main">variable height content here</div>
    <div id="sidebar">
       <div id="foo">...</div>
       <div id="bar">....</div>


fixHeights = function() {   
    var children_height = 0;  
    $('#sidebar'). children().each(function(){children_height += $(this).height();});
    $('#container').height(Math.max(children_height, $('#main').height()));


This is a very odd question, as div’s height is always the height of its children.

Are you floating content in your container div? When you float child content the containing div doesn’t act the same anymore.

If you’re floating content that extends past the bottom of the container div, add the following div to the very bottom of the children of the container div:

<div style="clear:both;"></div>

That will not allow children to float over it, thus forcing the containing div to be the height of its tallest child…

<div id="container">
  <div id="dynamic" style="float:left;width:100px;">dynamic content goes here</div>
  <div id="static" style="margin-left:104px;">Lots of static stuff here</div>
  <div style="clear:both;"></div>

Okay, I’m not sure why you’re doing the positioning the way you are, but I’ve done something similar for a website that had to look like a desktop application. I don’t believe there is any way to do this other than with javascript. Html documents are designed to flow, not be rigid. If you want to bail on the javascript, you’ll have to let go of the positioning styles and use your floating and clearing divs. Its not that horrible…


Best way to embed flash in html

There are too many method for embedding flash in html, which way is the best? Requirements are:

  • Cross-browser support
  • Support for alternative content (if flash is not supported by the browser)
  • Possibility to require a specific version of the flash player

I have been reading about SWFobject, has anyone used it/tested?


In a project I work on we use SWFobject which works like a charm, it allows you to check for a specific version and also display alternative content if flash is not supported.

var fn = function() {
        var att = { data:"flash.swf", width:"y", height:"x" };
        var par = { menu: "false", flashvars: "" };
        signUp = swfobject.createSWF(att, par);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..