aspose file tools*
The moose likes Java in General and the fly likes newInstance() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "newInstance() method" Watch "newInstance() method" New topic
Author

newInstance() method

Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi,
I have two classes C1 extends C2

When I use
Object instance1=C1.newInstance();
it gives me java.lang.InstantiationException
What is wrong? Please help,
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
I didn't know so I looked it up:
direct from Sun...
-----
public class InstantiationException
extends Exception
Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.
------
I would check your classes and make sure they are all clases nor interfaces or abstract.
hope that helps
Dave


Dave
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi, Dave,
Thanks for your prompt response.
C1 and C2 are concrete classes. The question is that if I delete super(); it is OK. what's wrong?

Please help,
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Is there a default constructor in C2?
that's the only thing that comes to mind quickly, sorry I can't be more helpful but I'm fairly new at Java. If you want to post some code I'd be happy to take a look at it for you.
Dave
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi, Dave,
It is not the default one. In fact, I am implementing a system which accept the input data from the user (argument type, value, and return type), the system organizes them into a testing program, and dynamically execute each java class in class library and compare the result with user's expected. public class
C1 class is here

C2 is this one

In the main,I use
String className="Isoceles"; //this is example
Class thisClass=Class.forName(className);
Object instance1=thisClass.newInstance();
it gives me java.lang.InstantiationException
What is wrong? Please help,
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You need an object of Class Class (don't you love it ) to use the newInstance method on.
Try
Object instance1=Class.forName("C1").newInstance();

"JavaRanch, where the deer and the Certified play" - David O'Meara
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi, Cindy,
Thank for your reply.
It was a typo. I did use
Class thisClass=Class.forName(className);
Object instance1=thisClass.newInstance();
Please check it for me.
Simon
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It works fine for me. Of course, a Shape class is necessary as well - I used
<code><pre>class Shape {
private String name;
public Shape() {}
public Shape(String name) {
this.name = name;
}
}
</pre></code>
as a minimal example.
I can't see any reason for this error. Is there any message associated with the exception? Did you double-check the line number from the stack dump to verify that it comes from the c1.newInstance() line? Also, try using this instead to see if you get the same error:
<code><pre>
Object instance0 = Isoceles.class.newInstance();
</pre></code>
Incidentally your formula for Area of a triangle is incorrect, though that should have nothing to do with your mysterious problem.

"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: newInstance() method