A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© simonqian 中级黑马   /  2013-6-4 11:32  /  903 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 simonqian 于 2013-6-4 14:21 编辑

  1. <P>package com.eight;

  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;


  5. //接口
  6. interface Hello
  7. {
  8.         void sayHello(String to);
  9.         void print(String s);
  10. }
  11. //接口的实现类
  12. class HelloImpl implements Hello
  13. {
  14.         public void print(String s)
  15.         {
  16.                 System.out.println("Print "+s);
  17.         }
  18.         public void sayHello(String to)
  19.         {
  20.                 System.out.println("Say Hello to "+to);
  21.         }
  22. }
  23. //生成与代理类相关的InvocationHandler对象
  24. class LogHandler implements InvocationHandler
  25. {
  26.         private Object obj;
  27.         public LogHandler(Object ojb)
  28.         {
  29.                 this.obj = obj;
  30.         }
  31.         public Object invoke(Object proxy, Method method, Object[] args)
  32.                         throws Throwable
  33.         {
  34.                 doBefore();
  35.                 Object res = method.invoke(obj, args);
  36.                 after();
  37.                 return res;
  38.         }
  39.         private void doBefore()
  40.         {
  41.                 System.out.println("before......!");
  42.         }
  43.         private void after()
  44.         {
  45.                 System.out.println("after......!");
  46.         }
  47. }
  48. public class proxyDemo
  49. {
  50.         public static void main(String[] args)
  51.         {
  52.                 HelloImpl helloImpl = new HelloImpl();
  53.                 LogHandler handler = new LogHandler(helloImpl);
  54.                 Hello hello =
  55.                         (Hello)Proxy.newProxyInstance(helloImpl.getClass().getClassLoader(),
  56.                                         helloImpl.getClass().getInterfaces(), handler);
  57.                 hello.print("All the test");
  58.                 hello.sayHello("Denny");
  59.         }
  60. }

  61. before......!
  62. Exception in thread "main" java.lang.NullPointerException
  63.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  64.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  65.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  66.         at java.lang.reflect.Method.invoke(Method.java:597)
  67.         at com.eight.LogHandler.invoke(proxyDemo.java:38)
  68.         at com.eight.$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Proxy0.print(Unknown Source)
  69.         at com.eight.proxyDemo.main(proxyDemo.java:60)</P>
  70. <P>为什么会出现上面的异常呀?
  71. </P>
复制代码

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

2 个回复

倒序浏览
public LogHandler(Object ojb)

        {

                this.obj = obj;//原来是这里写错了,哈哈

        }

回复 使用道具 举报
哈哈哈  自问自答的精神很可取
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马