Q. Jabber, it seems I have understood what you said. Can you give a more complicated example?

system (_)
<本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f >
A: Yes. After we declare a varable, we don't alway create a new object and assign it to the variable. Often, we get a referece to another Object by calling some other methed of some other Object, and then assign this refernce to our newly declared variable name.

//other code
Hashtable ht = new Hashtable();
ht.put("name", "John");
String s1= ( (String)ht.get("name") ).toUpperCase();
String s2= ( (String)ht.get("age") ).toUpperCase();

If you test this code, you will see "JOHN" being printed out and then an NullPointerException being thrown.

The reason is that you get a null as you try to get "age" from the Hashtable ht. Since it is a null, how can you call its toUpperCase() method? So the code fails at the last but two line.

In Java library, many motheds return an Object referece in the normal case and a null in the abnormal case. If the execution your code run into the abnormal case, the
NullPointerException brings you some trouble. This is where complexities arise.
<本文发表于: 相约加拿大:枫下论坛 www.rolia.net/f >

2000-9-4 -04:00

回到话题: Java Q&A (2) (compiled by jabber)

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论