File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reasons for java.lang .InstantiationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reasons for java.lang .InstantiationException" Watch "Reasons for java.lang .InstantiationException" New topic
Author

Reasons for java.lang .InstantiationException

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
Hi All,
I m getting java.lang.InstantiationException when trying to instantiate a class which following method
java.lang.Class c=class.forName("classname");
c.newInstance();

First line executes withour problem,but second line give an exception
java.lang.InstantiationException.

Can any one kinldy tell me what are the reasons when this exception is thrown
Cheers
-Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Surely the exception message gives you some hint as to what the reason might be... did you check that out, and if so, can you tell us? Also, did you think to look in the javadoc for the exception? It states "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.". Is this the case?

- Peter
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
Hi Peter,
Thanx a bunch for your reply.
The class im trying to instantiate is normal (final) class,but its parent class is abstract I could find only following details when running it thru my web application

java.lang.InstantiationException: com.x.olb3.process.finance.payment.AllocateFunds
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Unknown Source) .....
...
...
..

But when I tried to execute this thru simple java program I got following error...

Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at Test.main(Test.java:7)
Caused by: java.lang.NullPointerException
at com.x.olb3.process.Parent.<clinit>(Unknown Source)
... 3 more

But Strange thing is ..all other classes are subclass of this class and they are instansitated normally (in web app no thru java program)

any soln ??
Mani Venkatesan
Ranch Hand

Joined: Sep 15, 2002
Posts: 64
Could be that there is no zero-args constructor?

It could also be that your parent class constructor is calling an abstract method implementation which is in turn referring to some child class member not initialized yet.


Mani<br /><a href="http://ideanimal.com" target="_blank" rel="nofollow">blog</a>
vojdan kardalev
Greenhorn

Joined: Dec 11, 2010
Posts: 1
Mani Venkatesan wrote:Could be that there is no zero-args constructor?


I had no default construcor, it solved it for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Vojdan Karadalev, welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reasons for java.lang .InstantiationException