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
相關文章
- Object與Class的關係Object
- Cannot decode object of class Employee for key (NS.object.0); the class may be defined in source cod...Object
- scala class和object的區別Object
- Declaring Attributes of FunctionsFunction
- 訪問資料庫出現Object reference not set to an instance of an object錯誤資料庫Object
- Object C學習筆記18-SEL,@ selector,Class,@classObject筆記
- Scala進階(1)—— 反射 object 和 class反射Object
- object-c [self class] 和 [self _cmd]Object
- Restricting a Method to Class-Only o Instance-OnlyREST
- MIT6.0001 筆記,LECTURE 8:Object Oriented Programming (class,object,method)MIT筆記Object
- Java如何將Object轉換成指定Class物件JavaObject物件
- org.hibernate.TransientObjectException: object references an unsaved transient instance - save the tObjectException
- Java 中 instanceof 關鍵字 object instanceof ClassJavaObject
- iOS底層原理總結–instance、class、meta-calss物件的isa和superclassiOS物件
- iOS底層原理總結--instance、class、meta-calss物件的isa和superclassiOS物件
- Java中Class類與Object類之間有什麼關係?JavaObject
- 夯實Java基礎系列9:深入理解Class類和Object類JavaObject
- iOS底層原理 runtime-object_class拾遺基礎篇--(6)iOSObject
- Rank & Sort Loss for Object Detection and Instance Segmentation 論文解讀(含核心原始碼詳解)ObjectSegmentation原始碼
- sending context initialized event to listener instance of class org.springframework.web.context.ContContextZedSpringFrameworkWeb
- 為什麼 object_getClass(obj) 與 [OBJ class] 返回的指標不同Object指標
- TypeError: Cannot read private member xxx from an object whose class did not declare itErrorObject
- STL程式設計實踐七:儘量定義class形式的Function Object (轉)程式設計FunctionObject
- instance和clientclient
- Oracle database instanceOracleDatabase
- The Instance and the Database (285)Database
- STATUS OF ORACLE INSTANCEOracle
- Python函式是所謂的第一類物件(First-Class Object)是什麼鬼?Python函式物件Object
- iOS[super class]和[self class]iOS
- Uncaught TypeError: Object [object Object] has no method 'xxx'ErrorObject
- KVO後[obj class]與object_getClass(id obj)的結果竟會不一致?Object
- Typescript的interface、class和abstract classTypeScript
- 理解Database和InstanceDatabase
- Monitoring an SAP instance
- BIEE Instance OracleBIPresentat DownOracle
- Overview of Instance and Crash RecoveryView
- Oracle instance解釋Oracle
- self::class和static::class的區別