Announcement Announcement Module
No announcement yet.
Problem: java.lang.NullPointerException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem: java.lang.NullPointerException

    I have a problem to submit, I want to move from one page to another a name written by keyboard.
    Please help me is very important, I need for the thesis and I do not know where wrong.
    <form:form method="post" commandName="User">
    <td>First Name:</td>
    <td><input name="firstName" type="text" value="Harry"/></td>

    <td colspan="2">
    <input type="submit" value="Save Changes" />

    public class TerzaPagController extends SimpleFormController {

    private User user1;
    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView onSubmit(Object command)
    throws ServletException {

    String nome =((User) command).getFirstName();


    return new ModelAndView(new RedirectView(getSuccessView()));


    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    User user = new User();
    return user;


    <h1>QUARTA PAGINA</h1>
    <a href="<c:url value="hello.htm"/>">Torna a pagina iniziale</a>
    <p> il nome <c:out value="$nome"></c:out></p>


    public class QuartaPagController implements Controller {
    private User user1;
    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    String nome = user1.getFirstName();

    return new ModelAndView("quartapag", "nome", nome);



    <bean name="/terzapag.htm" class="springapp.web.TerzaPagController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="user"/>
    <property name="commandClass" value="springapp.web.User"/>
    <property name="formView" value="terzapag"/>
    <property name="successView" value="quartapag.htm"/>

    <bean id="user1" class="springapp.web.User">
    <property name="firstName"> <value>CICCIO</value>

    <bean name="/quartapag.htm" class="springapp.web.QuartaPagController"/>

  • #2
    I'm not in a position to try to actually compile and run your code, and it would be helpful to get a stack trace of your error. But, at a glance, your QuartaPagController looks like a problem -- you call "getFirstName()" on the User object, but there's nothing in place to assign a value to that object first.

    I'm assuming you want the formBackingObject from the first controller to magically become the formBackingObject of the second controller, and I'm not sure off the top of my head how you do that, but you have to do something explicit to make that happen.


    • #3
      This is the type of error.
      HTTP Status 500 -


      type Exception report


      description The server encountered an internal error () that prevented it from fulfilling this request.


      org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.p rocessRequest(
      org.springframework.web.servlet.FrameworkServlet.d oPost(
      javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
      javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

      root cause

      org.springframework.web.servlet.mvc.SimpleFormCont roller.onSubmit(
      org.springframework.web.servlet.mvc.SimpleFormCont roller.onSubmit(
      org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:267)
      org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl
      org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(
      org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda
      org.springframework.web.servlet.DispatcherServlet. doDispatch(
      org.springframework.web.servlet.DispatcherServlet. doService(
      org.springframework.web.servlet.FrameworkServlet.p rocessRequest(
      org.springframework.web.servlet.FrameworkServlet.d oPost(
      javax.servlet.http.HttpServlet.service(HttpServlet .java:710)
      javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

      note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.


      • #4
        OK, yeah -- you've got the same issue I described above in your TerzaPagController also. That is, you've got this object called user1, but you never assign it a User object, so it's null when you call setFirstName. I'm still not sure what you're trying to do, but you definitely need to get a User from somewhere and assign it to that variable before you can call anything on it.

        I think what you actually want to do here instead, rather than get the first name out of the command object and try to assign it to some new object, is simply to pass the command object on into the ModelAndView as the Model that the new view will use-- i.e. you want to formBackingObject from one view to become the new formBackingObject for the next view.


        • #5
          Ok, after try. What I want to do is to a page where you enter a name and by the submit pass this value to a second page that will print a video value, but not so good as done.
          I did something wrong?


          • #6
            Yes. As I said, what you want is to turn the formBackingObject from the first form into the Model in the ModelAndView that displays the second form.

            Another approach, of course, is to have both pages served by the same view/controller, and just show different information on the page depending on what is in the formBackingObject.

            If it's not clear what I mean above, then you need to revisit the documentation/tutorials. Small changes to your current approach are not going to be enough.