package reflect_noProxy;import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;public class TestS_para2 { public static void main(String[] args) throws SecurityException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
Student s_new = new Student(); Handle_invoker handle = new Handle_invoker(s_new);
Student_interface s_proxy = (Student_interface) Proxy.newProxyInstance(
s_new.getClass().getClassLoader(), s_new.getClass()
.getInterfaces(), handle);
//问题1:既然这里s_proxy====null,那么下边两行怎么不报NullPointerException??
//问题2: 而B那一行(s_proxy.equals(null)))怎么报了NullPointerException??
System.out.println("s_proxy====" + s_proxy);// s_proxy====null
System.out.println("s_proxy.toString====" + s_proxy.toString());// s_proxy.toString====null System.out.println("s_proxy.getid====" + s_proxy.getId());// s_proxy.getid====null
if (s_proxy == null) {
System.out.println("s_proxy==null");// 不执行到这里
}
if (null == s_proxy) {
System.out.println("null == s_proxy");// 不执行到这里
} //B:这里报异常
if (s_proxy.equals(null)) {//Exception in thread "main" java.lang.NullPointerException
System.out.println("s_proxy==null");
}
}
}
----------------------高手给解答呀!!谢谢了。
package reflect_noProxy; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy; public class TestS_para2 { public static void main(String[] args) throws SecurityException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
Student s_new = new Student(); Handle_invoker handle = new Handle_invoker(s_new);
Student_interface s_proxy = (Student_interface) Proxy.newProxyInstance(
s_new.getClass().getClassLoader(), s_new.getClass()
.getInterfaces(), handle);
//问题1:既然这里s_proxy====null,那么下边两行怎么不报NullPointerException??
//问题2: 而B那一行(s_proxy.equals(null)))怎么报了NullPointerException??
System.out.println("s_proxy====" + s_proxy);// s_proxy====null System.out.println("s_proxy.toString====" + s_proxy.toString());// s_proxy.toString====null System.out.println("s_proxy.getid====" + s_proxy.getId());// s_proxy.getid====null
if (s_proxy == null) {
System.out.println("s_proxy==null");// 不执行到这里 }
if (null == s_proxy) {
System.out.println("null == s_proxy");// 不执行到这里
} //B:这里报异常 if (s_proxy.equals(null)) {//Exception in thread "main" java.lang.NullPointerException
System.out.println("s_proxy==null");
}
}
}
System.out.println("s_proxy.toString====" + s_proxy.toString())System.out.println("s_proxy.getid====" + s_proxy.getId());
这两句肯定会抛出空异常的
s_proxy为null.
那s_proxy.getid肯定报错呀.
你想想.
s_proxy为null.
null.getid不报错?
s_proxy.equals(null)基本上不会用它判断null,字符串才有equals().