Reputation: 193
I am facing difficulty with the below code. I need to create object through reflection by class name in Java but it throws below error while compiling.
// Class where package name with class is coming
public class PickAdapter<T> {
Object mObject;
public T read(Element element, String classOfName) {
try {
Class mClass = Class.forName(classOfName);
mObject = mClass.newInstance();
Field[] fd = mClass.getDeclaredFields();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return (T) mObject;
}
}
Error in Logcat:
10-28 17:12:54.619 20743-20743/com.harpz.htmlee D/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err: java.lang.ClassNotFoundException: Invalid name: class com.harpz.htmlee.model.MUser
10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err: at java.lang.Class.classForName(Native Method)
10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err: at java.lang.Class.forName(Class.java:400)
10-28 17:12:55.771 20743-20743/com.harpz.htmlee W/System.err: at java.lang.Class.forName(Class.java:326)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.harpz.htmleetim.reflection.PickAdapter.read(PickAdapter.java:22)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:25)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:19)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.harpz.htmleetim.Htmlee.fromHtml(Htmlee.java:13)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.harpz.htmlee.MainActivity.onCreate(MainActivity.java:40)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.Activity.performCreate(Activity.java:6679)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.os.Looper.loop(Looper.java:154)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
10-28 17:12:55.772 20743-20743/com.harpz.htmlee W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Upvotes: 2
Views: 2176
Reputation: 87
To load a class using reflection you need to ensure, that the wanted class is available in your Classpath.
In case you are developing an Android-App you need to include external packages into your apps library path.
To load the class you need to specify the fully qualified class name, that means including the package name, e.g. com.example.packageName.ClassName
So from your provided log output i can see that you try to load the class com.harpz.htmlee.model.MUser
But, as the error message suggests, you try to load the class with the name class com...
.
Remove the preceding class (+ whitespace) keyword within the passed classOfName
variable.
Moreover, i would advise you to change the mObject
attribute into a locale variable within the read
Method, as your usage (bases on your given snippet) is only within this method.
Furthermore, the return
statement should be located within the try
-block as it would never be reached in case of any exception.
So the code improvements would look like this:
public class PickAdapter<T> {
public T read(Element element, String classOfName) {
Object mObject;
try {
Class mClass = Class.forName(classOfName);
mObject = mClass.newInstance();
Field[] fd = mClass.getDeclaredFields();
return (T) mObject;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
} // end of class
Upvotes: 3