• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

InstantiationException

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
One of the questions of J2EECertification--
Assume the following class and interface, defined in different source files and same package, will the following JSP tag work? <jsp:useBean id="mobile" scope="session" class="Device" type="Phone" />

1. public interface Device {
2. }

1. public class Phone implements Device{
2. }

1. Yes. The phone class implements correctly the Device interface.
2. No. The class attribute Device is not a superclass of the class Phone.
3. No. The attribute class and type cannot be used together.
4. Yes. The useBean Tag is implemented correctly.
5. No. An InstantiationException is thrown at compile-time.

Answer 2 is correct.

My doubt is what change in the above code could cause an InstantiationException?

Thanks
Deepti
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean id="mobile" scope="session" class="Device" type="Phone" />

In the above statement if there is no object value set to the mobile attribute .
Then it will cause instantiation Exception as in then the container will try to create an instance of Device wich is an interface.

code that follows is


Phone mobile = (Device) pageContext.getAttribute("mobile",PageContext.PAGE_SCOPE);

if(mobile=null)

{
Phone mobile = new Device() //This will cause error if there is no mobile attribute already set bcos of if condition

}
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think deepti ,you are correct
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested the code just like Deepti said but I did not get Instantiation Exception.It is just a JspException.

So I think question remains unanswered as Deepti asked for the change in the code that would cause the IE.

Thanks,
Amol
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code wouldn't throw any exception from knowledge, it will rather show a compilation error like : "Can't instantiate the Device", as we can't create a object of interface type e,g. new Device().

And here the changes made to get java.lang.InstantiationException:
1:Remove class attribute from <jsp:useBean>
<jsp:useBean id="mobile" scope="request" type="bean.Device" />

2: And set attribute of type Device somewhere in your jsp or servlet like :
in a jsp which will forward request to the another jsp where jsp:useBean has been used.
<%
Phone mobile = new Phone();
session.setAttribute("mobile",mobile) ;
%>

Now if run this we will definetly get and InstantiationException as we set the attribute in "session" and trying to get it in "request" scope and we dont have a class attrribute.
This all changes successfully get back youaInstantiationException ,which I think required by you.
 
deepti bellubbi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it. Thank you all.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic