Re: instantiation exception
Can you post more details? What is the name of the classs?
What class and methods are you trying to use to load that class?
Re: instantiation exception
Quote:
Originally Posted by
Norm
Can you post more details? What is the name of the classs?
What class and methods are you trying to use to load that class?
tnx for ur reply
i used this code:
public static void main(){
outer o=new outer();
inner i=o.new inner ();
i=(inner)Class.forName("a.outer$inner").newInstanc e();
}
the error was instantiation exception
Re: instantiation exception
Can you make a small complete program that compiles, executes and shows the problem?
Be sure to wrap the code in code tags. What you posted won't compile or execute.
Re: instantiation exception
<code>
package a;
public class A {
public A(){System.out.println("a constructed");}
public class B{
public B(){System.out.println("b constructed");}
}
}
--------------------------------------------
package helloworld;
import a.A;
import a.A.B;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
A a=new A();
B b=a.new B();
b=(B)Class.forName("a.A$B").newInstance();
}
}
</code>
---------------------------------
output is:
a constructed
b constructed
Exception in thread "main" java.lang.InstantiationException: a.A$B
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at helloworld.Main.main(Main.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
--- Update ---
[CODE]
Quote:
package a;
public class A {
public A(){System.out.println("a constructed");}
public class B{
public B(){System.out.println("b constructed");}
}
}
package helloworld;
import a.A;
import a.A.B;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
A a=new A();
B b=a.new B();
b=(B)Class.forName("a.A$B").newInstance();
}
}
Re: instantiation exception
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Can you remove the packages. They make the testing too hard to setup.
Re: instantiation exception
Quote:
Originally Posted by
Norm
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
ok, really Sorry
output:
a constructed
b constructed
Exception in thread "main" java.lang.InstantiationException: a.A$B
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at helloworld.Main.main(Main.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Re: instantiation exception
Have you researched if this is possible?
Re: instantiation exception
Quote:
Originally Posted by
Norm
Have you researched if this is possible?
I have a code that has run before, but now i cant run it. i know there should be a way...
--- Update ---
HooRAY, I found the way
--- Update ---
Dear Norm,
really tnx for ur attention and reminding me to research;-)
Re: instantiation exception
Thanks for sharing the solution.