`

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 简单教程

    一个ant的简单的教程,很适合基础的入门

    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 ...

    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 ...

    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 ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    Building an Object Model with the Class Designer 778 Summary 783 36 Sampling Design Patterns in C# 784 Overview of Design Patterns 784 The Iterator Pattern 785 Implementing the Proxy ...

    OSGI in Action

    1.2 An architectural overview of OSGi 9 The OSGi framework 9 ■ Putting it all together 12 1.3 “Hello, world!” examples 12 Module layer example 12 ■ Lifecycle layer example 14 ■ Service layer ...

    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 ...

    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................

    JavaEE 5.0 Tutorial.pdf

    Sun 官方 J2ee 5.0 教程 The Java EE 5Tutorial For Sun Java System Application Server 9.1 Contents Preface ..................................................................................................

Global site tag (gtag.js) - Google Analytics