My code is:
import java.lang.reflect.Method;
public class ReflectionHelloWorld1 {
public static void main(String[] args){
//create instance of "Class"
Class<?> c = null;
try{ c=Class.forName("myreflection.Foo"); }
catch(Exception e){ e.printStackTrace(); }
//create instance of "Foo"
Foo f = null;
try { f = (Foo) c.newInstance(); }
catch (Exception e) { e.printStackTrace(); }
f.print();
} }
class Foo {
public void print() { System.out.println("abc"); }
}
I am getting this error:
java.lang.ClassNotFoundException: myreflection.Foo
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at a2.ReflectionHelloWorld1.main(ReflectionHelloWorld1.java:9)
java.lang.NullPointerException
at a2.ReflectionHelloWorld1.main(ReflectionHelloWorld1.java:18)
Exception in thread "main" java.lang.NullPointerException
at a2.ReflectionHelloWorld1.main(ReflectionHelloWorld1.java:23)
How to get rid of it? I want to know how to change settings to violate the concept of encapsulation in java during the compilation of reflection concept code.
Aucun commentaire:
Enregistrer un commentaire