Results 1 to 6 of 6

Thread: Problem: java.lang.NullPointerException

  1. #1
    Join Date
    Sep 2008
    Posts
    17

    Default 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.
    terzapag.jsp
    <form:form method="post" commandName="User">
    <table>
    <tr>
    <td>First Name:</td>
    <td><input name="firstName" type="text" value="Harry"/></td>
    </tr>

    <tr>
    <td colspan="2">
    <input type="submit" value="Save Changes" />
    </td>
    </tr>
    </table>
    </form:form>

    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();

    user1.setFirstName(nome);


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

    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    User user = new User();
    user.setFirstName("prova");
    return user;
    }

    }


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

    </body>
    </html>

    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>

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

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

  2. #2

    Default

    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. #3
    Join Date
    Sep 2008
    Posts
    17

    Default

    This is the type of error.
    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

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

    exception

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


    root cause

    java.lang.NullPointerException
    springapp.web.TerzaPagController.onSubmit(TerzaPag Controller.java:25)
    org.springframework.web.servlet.mvc.SimpleFormCont roller.onSubmit(SimpleFormController.java:409)
    org.springframework.web.servlet.mvc.SimpleFormCont roller.onSubmit(SimpleFormController.java:381)
    org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:267)
    org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:265)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:153)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:809)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:511)
    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. #4

    Default

    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. #5
    Join Date
    Sep 2008
    Posts
    17

    Default

    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. #6

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •