`

object is not an instance of declaring class

阅读更多
在使用反射执行一个方法时常遇到object is not an instance of declaring class的异常,如下代码:view plaincopy to clipboardprint?
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

 

分享到:
评论

相关推荐

    大数据关键技术与挑战

    大数据是信息技术领域的一个重要分支,它涉及到数据的采集、存储、管理、分析与应用等多个方面。随着互联网、物联网、云计算等技术的发展,数据的规模和复杂性呈爆炸性增长,对数据处理技术提出了更高的要求。...

    ant 简单教程

    知识点:Apache Ant 简介与使用指南 一、引言 Apache Ant 是一款由 Apache Software Foundation 开发的 Java 基础构建工具。这款工具的显著特点在于其构建文件采用 XML 格式编写,充分利用了 XML 的开放标准、便携...

    JSP Simple Examples

    To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file. Setting Colors ...

    java面试题英文版及其答案

    12. Explain the concept of polymorphism in Java.Answer: Polymorphism is the ability of an object to take on many forms. In Java, it is achieved through method overriding and method overloading. Method...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having ...

    IOS5 Programming Cookbook

    - **Project Structure**: Understanding the structure of an Xcode project is crucial. Projects typically consist of files such as source code files (.m), interface definition files (.h), resource files...

    Sakemail

    Fixed a bug when sending email to more than two address (the separator is still ‘,‘).9/3/981.6.0- Sometimes the filenames of an attachment contain invalid chars making very dificult to open a ...

    ZendFramework中文文档

    Fetching a Row as an Object 10.3. Zend_Db_Profiler 10.3.1. Introduction 10.3.2. Using the Profiler 10.3.3. Advanced Profiler Usage 10.3.3.1. Filter by query elapsed time 10.3.3.2. Filter by ...

    Objective-C 基础教程

    - **Static Typing to an Inherited Class(对继承类进行静态类型)**:解释了如何将静态类型应用于继承的类。 综上所述,Objective-C基础教程覆盖了从入门到进阶的多个方面,适合Objective-C初学者系统学习该语言...

    spring-framework-reference4.1.4

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    spring-framework-reference-4.1.2

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................

Global site tag (gtag.js) - Google Analytics