Android/Java using reflection to select a class to create -


i trying learn more more complicated android/java techniques, , have come cropper on reflection code.

i tried make simple example - create gamepieces or enemypieces object depending on integer entered.

by using log statements can see constructor found , correctly named - still gives me nullpointer error assuming because objects not created. have gone round houses several times. point out fundamental error or me find going wrong please.

--my main class

    package com.doka.testreflect;     import com.doka.tokens.gamepieces;     import android.os.bundle;     import android.app.activity;     import android.util.log;     public class mainactivity extends activity {         @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);             tokenfactory tf = new tokenfactory();             gamepieces tok;             (int i=0; i<4; i++){                 tok = tf.maketoken(i);                 log.d("debugmessage", "made "+tok.number);             }             }             } 

-- (try to) make objects using reflection

    package com.doka.testreflect;     import java.lang.reflect.constructor;     import com.doka.tokens.gamepieces;     public class tokenfactory {         final string package_location = "com.doka.tokens.";          public gamepieces maketoken (int i) {             class[] consargs = {int.class, string[][].class};             constructor constructor;              try{         constructor = class.forname( package_location+gettype(i) )                     .getconstructor(consargs);         return (gamepieces) constructor.newinstance(i);             }             catch (exception se){return null;}                 }                      //here try select 1 of 2 different objects                 private string gettype(int i){             if (i==1) return "enemypieces";             else return "gamepieces";                 }                     } 

--the simple test class

    package com.doka.tokens;     public class gamepieces {     public int number;         public gamepieces (int i){            number = i;         }     } 

--the subclass

    package com.doka.tokens;     public class enemypieces extends gamepieces{         public enemypieces(int i) {            super(i);         }     } 

i error when try print object data logcat. @ line (log.d("debugmessage", "made "+tok.number);). can print out constructor - try block failing when tries return constructor.newinstance (i think). logcat

05-03 10:32:38.294: e/androidruntime(537): fatal exception: main 05-03 10:32:38.294: e/androidruntime(537): java.lang.runtimeexception: unable start activity componentinfo{com.doka.testreflect/com.doka.testreflect.mainactivity}:     java.lang.nullpointerexception 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread.performlaunchactivity(activitythread.java:1815) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread.handlelaunchactivity(activitythread.java:1831) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread.access$500(activitythread.java:122) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread$h.handlemessage(activitythread.java:1024) 05-03 10:32:38.294: e/androidruntime(537):  @ android.os.handler.dispatchmessage(handler.java:99) 05-03 10:32:38.294: e/androidruntime(537):  @ android.os.looper.loop(looper.java:132) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread.main(activitythread.java:4123) 05-03 10:32:38.294: e/androidruntime(537):  @ java.lang.reflect.method.invokenative(native method) 05-03 10:32:38.294: e/androidruntime(537):  @ java.lang.reflect.method.invoke(method.java:491) 05-03 10:32:38.294: e/androidruntime(537):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841) 05-03 10:32:38.294: e/androidruntime(537):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:599) 05-03 10:32:38.294: e/androidruntime(537):  @ dalvik.system.nativestart.main(native method) 05-03 10:32:38.294: e/androidruntime(537): caused by: java.lang.nullpointerexception 05-03 10:32:38.294: e/androidruntime(537):  @ com.doka.testreflect.mainactivity.oncreate(mainactivity.java:19) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activity.performcreate(activity.java:4397) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1048) 05-03 10:32:38.294: e/androidruntime(537):  @ android.app.activitythread.performlaunchactivity(activitythread.java:1779) 05-03 10:32:38.294: e/androidruntime(537):  ... 11 more 


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -