File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reasons for java.lang .InstantiationException

 
Praful Thakare
Ranch Hand
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vojdan kardalev
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vojdan Karadalev, welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic