P
pek
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?
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?