本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
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.
更多精彩文章及讨论，请光临枫下论坛. 网址: rolia.net/zh