advice needed on checking for null object references

A

Arne Vajhøj

I find Java's sloppy distinctions between null and empty needs some
work. See http://mindprod.com/jgloss/empty.html

Java has very well defined distinction between null and empty string.
> Java is going to check it for you anyway. So you can count on stack
> dump and termination.
There is not too much point in

assert x != null : "x null";

since that will happen anyway.

There is absolutely point.

There is always point in getting the exception where the
problem arise instead of some random place further ahead/down
the code.

Arne
 
L

Lew

That will most assuredly not happen anyway. What will happen "anyway" is a
NullPointerException, not an assertion failure.
There is absolutely point.

There is always point in getting the exception where the
problem arise instead of some random place further ahead/down
the code.

Exactly so. Furthermore, there is a world of difference between an assertion
and an exception. Exceptions enforce invariants; they cause them to be true.
Assertions do not. Assertions prove and document invariants, but they are
explicitly incapable of enforcing them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top