Checking for null parameter



So is checking for null pointer a good practice? I mean, 90% of the
times you have any object as a parameter, you really want that object
to be something other than null. Otherwise you have to blot your code
into an unlimited number of lines of try, catch etc (and sometimes you
will fail doing any correct error handling).

Just to make my question more clear, here is an example:

class Test {
private A a;
public Test(A a) throws Exception {
if ( a == null ) throw new Exception("Null!");
this.a = a
}
}

class Hell {
private Devil d;
private Fire f;
public Hell(Devil d, Fire f) throws Exception {
if ( ( d == null ) || ( f == null ) ) throw new
Exception("Null!");
this.d = d; this.f = f;
}
public void burnAngel(Angel a) {
if ( a == null ) throw new Exception("Null!");
a.burn();
}
}

class Main {
public static void main(String[] args) {
try {
Test t = new Test(new A());
} catch (Exception e) {}
try {
Hell h = new Hell(new Devil(), new Fire());
} catch (Exception e){}
}
}

So, is this all necessary? I mean, if A, Devil, Fire and Angel is null
and I know that they should not be, then that means that I have an
error in my code. There is no reason to explicity tell someone that
the object should not be null. If I do, I have to try catch almost 80%
of my code.

What do you think? If checking for null overkill or not?
.



Relevant Pages

  • Re: Checking for null parameter
    ... private Devil d; ... Fire f) throws Exception { ... Hell h = new Hell(new Devil(), ...
    (comp.lang.java.programmer)
  • Re: Checking for null parameter
    ... If it is truly an error to pass in null to a method, allow the JVM to product a null-pointer exception. ... Fire f) throws Exception { ... Hell h = new Hell(new Devil(), ...
    (comp.lang.java.programmer)
  • Re: Checking for null parameter
    ... Dereferencing 'a' in above example would throw that exception ... private Devil d; ... Fire f) throws Exception { ... Hell h = new Hell, ...
    (comp.lang.java.programmer)
  • Re: Checking for null parameter
    ... Dereferencing 'a' in above example would throw that exception ... private Devil d; ... Fire f) throws Exception { ... Hell h = new Hell, ...
    (comp.lang.java.programmer)
  • Re: realtime rl v.2
    ... You have to make a choice between a Rendering Engine knowing how to render objects of different type, or each object knowing how to render itself to a surface. ... If you want to use the second, the surface should be separate from the object and can be used like Object.Render;. ... Start with (Private Properties, Public Properties, Constructors, Private/Protected Methods, Public Methods; for forms add Form Events and Control Events). ... To find it yourself, click Debug->Exceptions and then find the above exception, click the left check box and ok. ...
    (rec.games.roguelike.development)