• 使用反射报异常:object is not an instance of declaring class解决方案


    在使用反射执行一个方法时常遇到object is not an instance of declaring class的异常,如下代码:view plaincopy to clipboardprint?

    Java代码

    import java.lang.reflect.Method;    
    import java.text.SimpleDateFormat;    
    import java.util.Date;    
       
       
       
    import cn.rdt.famework.frame.config.FrameConstant;    
       
    public class PrimaryKeyUtils {    
     //    
        public    synchronized String getPrimaryKey() {    
            String pk = "";    
            StringBuffer primaryKey = new StringBuffer(new SimpleDateFormat(    
                    "yyMMddHHmmssSSS").format(new Date()));    
            int tpk = FrameConstant.PRIMARY_KEY;    
            if (tpk < 9999) {    
                tpk++;    
            } else {    
                tpk = 1000;    
            }    
            FrameConstant.PRIMARY_KEY = tpk;    
            pk = primaryKey.append(String.valueOf(tpk)).toString();    
            primaryKey = null;    
             
            return pk;    
        }    
       
        public String GetPrimaryKey(String mothed){    
            String primaryKey = "";    
            try {    
                Class c = PrimaryKeyUtils.class;    
                Method m = c.getMethod(mothed,new Class[]{});    
    //          Object obj=c.newInstance();    
                m.invoke(mothed,null);    
                primaryKey = String.valueOf(m.invoke(c.newInstance() ,new Object[]{}));    
            } catch (Exception e) {    
                e.printStackTrace();    
            }     
            return primaryKey;    
        }    
        public static void main(String[] args) {    
             
            PrimaryKeyUtils primaryKey = new PrimaryKeyUtils();    
            System.out.println(primaryKey.GetPrimaryKey("getPrimaryKey"));    
        }    
    }   
    import java.lang.reflect.Method; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 
     
     
     
    import cn.rdt.famework.frame.config.FrameConstant; 
     
    public class PrimaryKeyUtils { 
     // 
        public    synchronized String getPrimaryKey() { 
            String pk = ""; 
            StringBuffer primaryKey = new StringBuffer(new SimpleDateFormat( 
                    "yyMMddHHmmssSSS").format(new Date())); 
            int tpk = FrameConstant.PRIMARY_KEY; 
            if (tpk < 9999) { 
                tpk++; 
            } else { 
                tpk = 1000; 
            } 
            FrameConstant.PRIMARY_KEY = tpk; 
            pk = primaryKey.append(String.valueOf(tpk)).toString(); 
            primaryKey = null; 
          
            return pk; 
        } 
     
        public String GetPrimaryKey(String mothed){ 
            String primaryKey = ""; 
            try { 
                Class c = PrimaryKeyUtils.class; 
                Method m = c.getMethod(mothed,new Class[]{}); 
    //          Object obj=c.newInstance(); 
                m.invoke(mothed,null); 
                primaryKey = String.valueOf(m.invoke(c.newInstance() ,new Object[]{})); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            }  
            return primaryKey; 
        } 
        public static void main(String[] args) { 
          
            PrimaryKeyUtils primaryKey = new PrimaryKeyUtils(); 
            System.out.println(primaryKey.GetPrimaryKey("getPrimaryKey")); 
        } 

      
     
     第34行会报object is not an instance of declaring class错 对象不是声明类的一个实例。解决办法如下: 
     
    第一种:反射执行的方法 getPrimaryKey() 改成静态的 
     
    第二种:在执行方法前先实例化类。m.invoke(mothed,null)改为m.invoke(c.newInstance(),null)或者m.invoke(new PrimaryKeyUtils(),null) 
     
     
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yeson6/archive/2011/01/14/6138963.aspx 

  • 相关阅读:
    本地快速搭建 FTP 服务器
    css 四个角
    时间
    两个json深度对比
    工作常用
    js模块化 中的变量可在局部 中的‘全局共享’
    datatables 的导出button自定义
    css布局技巧
    datables自定义排序
    js判断是否为空 或者全部为空
  • 原文地址:https://www.cnblogs.com/ningxu/p/3392112.html
Copyright © 2020-2023  润新知