object is not an instance of declaring class

悠悠隱於市發表於2011-04-02
在使用反射執行一個方法時常遇到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

 

相關文章