＜本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f ＞
Thanks a million for long-river and guest's posting. My postings are essentially for laymen and novices. Obviously, NullPointerException is a trivial one and
can be easily handled by a true Java programmer.
Guest pointed out the general solution:
Check if the Object reference is null before
you call its methods. For instance, people
need check if a network connection or dababase connection is still there before you call its corresponding close() method.
Another kinds of example is related to some
deprecated methods, which usually return a null in a server that implements a higher version of API. For instance, in most Java servlet engine that implements servlet API 2.2, if you run a servlet that uses getServlet method of the ServletContext interface, you will get a null. In the server-side Java programming, you don't always have luck to
see the StackTrace of a NullPointerException.
It seems to me nothing is really simple in this world. I have ever witnessed many Java learners stunned before NullPointerException. For a beginner, it is not straightforward to understand NullPointerException, let alone handling it gracefully.＜本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f ＞