• 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

getting NullPointerException after changing scope of form as request

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using Struts and am trying to display and edit a list of objects using nested iterate. However when I submit the form, the form is not getting populated causing the following exception.

I am getting this exception when I set the scope of the action to request. However the same code is working properly when I set the scope of the action to session instead of request.

java.lang.NullPointerException
at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:515)
at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:428)
at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:750)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:881)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)
at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)

Thanks in advance!

[ June 13, 2008: Message edited by: vaibhav maddiwar ]
[ June 13, 2008: Message edited by: vaibhav maddiwar ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use indexed properties on a form in request scope, you must use "Lazy initialization" techniques for the indexed getters. This article explains more about this.
 
vaibhav maddiwar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Merrill for your response!

Actually I am not using List rather I am using Array of VO's as a form property. Also I am getting NullPointerException but not ArrayIndex....

Here is my code...



FISurveyForm.java




SurveyResultsVO.java ...

This is my value object








fiSurveyResults.jsp ...

Here is jsp with my iterate logic



Any help is appreciated..

Thanks in Advance!
[ June 16, 2008: Message edited by: vaibhav maddiwar ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you're using an array or a List, the problem is basically the same: When an ActionForm is in request scope any elaborate structure of nested objects that you created when the page was rendered is gone when the user submits the page back to the server. We now have a completely different HTTPServletRequest object than we had when the page was rendered. Struts will automatically instantiate a new Form bean and put it in request scope, but this new form bean contains no array of questions. That's what's causing your null pointer exception: The array of objects you're expecting to be in the ActionForm is null. In an application such as yours where you appear to have a preset list of questions, you have the following choices:
  • Keep the ActionFrom in Session scope
  • Use the ActionForm's reset method to rebuild the list of questions after the user submits the form
  • Use the Lazy Initialization techniques in the article I referenced earlier
  •  
    vaibhav maddiwar
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Merrill!

    Thanks for your time!

    I tried to introduce Lazy Initialization Technique but now i am getting InvocationTargetException.

    Here is my updated form



    Here is the error message which I am getting....

    java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:514)
    at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:428)
    at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:750)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)
    at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:881)
    at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
    at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)
    at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)
    at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
    Caused by: java.lang.ClassCastException
    at com.bofasecurities.aps.form.FISurveyForm.getLstResultsVO(FISurveyForm.java:208)


    Please advice!

    Thanks
    Vaibhav
     
    Merrill Higginson
    Ranch Hand
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You will have to give the questionScores array the same type of lazy initialization behavior. since your SurveyResultsVO doesn't extend AcionForm, you will have to use the constructor rather than the reset method.

    Also, I'm wondering if it's the fact that you're converting to and from an array that's causing the problem. You might try making the getters and setters take List parameters.
    [ June 16, 2008: Message edited by: Merrill Higginson ]
     
    vaibhav maddiwar
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Merrill,

    I even tried with giving the questionScores array the same type of lazy initialization behavior but still facing with same issue..."InvocationTargetException"
    Also I couln't make the getters and setters take List parameters because I have QuestionScoreVO as one of the property SurveyResultsVO.

    Could you tell me where I am wrong

    Thanks,
    Vaibhav
     
    Merrill Higginson
    Ranch Hand
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't see any indexed getters in your classes. I'm not sure how your code worked before even in session scope without them, but you will for sure need them in request scope. Example:
     
    reply
      Bookmark Topic Watch Topic
    • New Topic