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
相關文章
- Ruby class_eval and instance_eval notes
- 訪問資料庫出現Object reference not set to an instance of an object錯誤資料庫Object
- Object與Class的關係Object
- org.hibernate.TransientObjectException: object references an unsaved transient instance - save the tObjectException
- MIT6.0001 筆記,LECTURE 8:Object Oriented Programming (class,object,method)MIT筆記Object
- Scala進階(1)—— 反射 object 和 class反射Object
- Java 中 instanceof 關鍵字 object instanceof ClassJavaObject
- Java如何將Object轉換成指定Class物件JavaObject物件
- iOS底層原理總結–instance、class、meta-calss物件的isa和superclassiOS物件
- iOS底層原理總結--instance、class、meta-calss物件的isa和superclassiOS物件
- TypeError: Cannot read private member xxx from an object whose class did not declare itErrorObject
- Rank & Sort Loss for Object Detection and Instance Segmentation 論文解讀(含核心原始碼詳解)ObjectSegmentation原始碼
- vertx instance
- GPU InstanceGPU
- iOS底層原理 runtime-object_class拾遺基礎篇--(6)iOSObject
- 夯實Java基礎系列9:深入理解Class類和Object類JavaObject
- 3.2.1 Mounting a Database to an InstanceDatabase
- 3.1.5.6 Forcing an Instance to Start
- C++ instance的使用C++
- RMAN Duplicate RAC to Single Instance
- 3.1.5.2 Starting an Instance, and Mounting and Opening a DatabaseDatabase
- Property [title] does not exist on this collection instance
- KVO後[obj class]與object_getClass(id obj)的結果竟會不一致?Object
- iOS[super class]和[self class]iOS
- ES7 Object.keys,Object.values,Object.entriesObject
- ObjectObject
- class
- 介面返回[object,Object]解決方法Object
- Typescript的interface、class和abstract classTypeScript
- Zero-shot Learning零樣本學習 論文閱讀(一)——Learning to detect unseen object classes by between-class attributeObject
- LGWR (ospid: 29534): terminating the instance due to error 4021Error
- NSCFType unrecognized selector sent to instance等問題Zed
- TypeError: The ‘compilation‘ argument must be an instance of Compilation 報錯Error
- MXRuntimeUtils,替代 [NSObject performSelector object object ]的工具ObjectperformSelector
- self::class和static::class的區別
- 全網最適合入門的物件導向程式設計教程:32 Python 的內建資料型別-類 Class 和例項 Instance物件程式設計Python資料型別
- Object流Object
- object類Object
- Object.seal()與Object.freeze()區別Object