This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

InstantiationException!!!  RSS feed

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
i have the following JSP codes:

i have placed this jsp and class file as follows:
TOMCAT_HOME\webapps\RnD\messageCenter.jsp
TOMCAT_HOME\webapps\RnD\WEB-INF\classes\com\trai\User.class
The messageCenter.jsp is invoked with 'user' passed to it and throws the following exception which i don't know how to solve:
org.apache.jasper.JasperException: class com.trai.User : java.lang.InstantiationException: com.trai.User
I would appreciate if you could tell me why the User.class can't be instantiated? what could be the possible situation that causes this problem?
thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class probably doesn't have a public default (no-argument) constructor, so that the JSP can't create one automatically.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will supply that automatically, provided that you haven't defined any constructors of your own.
BUT ... is your class declared public? I think it needs to be.
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ernest and Ron. It did work after i explicitly created a default constructor. but how did you figure out that I needed to have a default constructor As looking at my code, i did find any place where this difault constructor has been invoked.
thanks.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should NOT have to write a default constructor, unless you've also written other constructors that take arguments.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathi -
Your code doesn't invoke it, the servlet container does in response to the <useBean> tag. The container has to create the object somehow, right? The only thing it can try is to use the default constructor.
Ron -- yes indeed, the compiler will supply a default constructor if you don't write any yourself, but if you do have some explicit ones, then it won't, of course. That's what I was assuming (rightly, apparently) here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!