编辑用户信息时为NullPointerExeception

问题描述 投票:-1回答:1

我正在尝试编辑存储在mysql中的用户信息,但是我无法绑定数据并且无法获得mysql中更改数据的结果

以下代码:

PassegerController

  @RequestMapping(value="/updateprofile", method= {RequestMethod.POST}) 
  public ModelAndView editProfile(HttpServletRequest request, HttpServletResponse response,
          @Valid @ModelAttribute("customer")Passenger newUser, 
          BindingResult result, ModelMap model)   {

        //JPA persistent
        factory = Persistence.createEntityManagerFactory("TaghreedCOMP303_Assignment2");
        UserRegistry = factory.createEntityManager();

        //Creating HttpSession object to store the unique ID of the user

        HttpSession session = request.getSession();

        //Storing the user information by using setAttribute

        int passengerId = 0;
        if(session.getAttribute("passengerId") !=null);
            passengerId=Integer.parseInt(session.getAttribute("passengerId").toString());

        String address = request.getParameter("address");
        String city = request.getParameter("city");
        String postalCode= request.getParameter("postalCode");
        String country = request.getParameter("country");

        UserRegistry.getTransaction().begin();
        Passenger RegistredCustomer = new Passenger();

        RegistredCustomer= UserRegistry.find(Passenger.class, passengerId);
        //Inserting passenger updated data into Passengers table
        RegistredCustomer.setAddress(address);
        RegistredCustomer.setCity(city);
        RegistredCustomer.setPostalCode(postalCode);
        RegistredCustomer.setCountry(country);

        UserRegistry.persist(RegistredCustomer);
        UserRegistry.getTransaction().commit();
        UserRegistry.close();

      String message="Successfully updated";
        return new ModelAndView("viewprofile","RegistredCustomer",RegistredCustomer);
  }

我正在尝试编辑存储在mysql中的用户信息,但是我无法绑定数据并且无法获得mysql中更改数据的结果

以下代码:

  <form:form action="updateprofile" method="post"  modelAttribute="customer"
        style="padding:2px;background:#F0F8FF;width:50%;border-top:2px solid #009;
        border-bottom:2px solid #009;border-right:2px solid #009;border-left:2px solid #009;margin-left:350px;border-radius:10px">
       <h4 style="text-shadow:2px 2px 3px rgba(150,150,150,0.75)">Profile Information:</h4>
            <table align="center">
                <tr>
                    <td>
                        <form:label path="userName">User Name</form:label>
                    </td>
                    <td>
                        <form:input path="userName" type="text" name="userName" value="${RegistredCustomer.userName}" disabled="true"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <form:label path="address">Address</form:label>
                    </td>
                    <td>
                        <form:input path="address" type="text" name="address" value="${RegistredCustomer.address}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <form:label path="city">City</form:label>
                    </td>
                    <td>
                        <form:input path="city" type="text" name="city" value="${RegistredCustomer.city}"  />
                    </td>
                </tr>
                <tr>
                    <td>
                        <form:label path="country">Country</form:label>
                    </td>
                    <td>
                        <form:input path="country" type="text" name="country" value="${RegistredCustomer.country}" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Postal Code</label>
                    </td>
                    <td>
                        <form:input path="postalCode" type="text" name="postalcode" value="${RegistredCustomer.postalCode}" />
                    </td>
                </tr>
                  <tr>
                    <td></td>
                    <td>
                        <button id="save" name="submit" style="width:100%;
                        border-radius:10px;text-shadow:2px 2px 3px 
                        rgba(150,150,150,0.75);font-family:time">Save</button>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button type="reset" id="reset" name="reset" 
                        style="width:100%;border-radius:10px;text-shadow:2px 2px 3px 
                        rgba(150,150,150,0.75);font-family:time">Clear</button>
                    </td>
                </tr>
                <tr></tr>
                <tr>
                    <td></td>
                    <td><a href="index.jsp">Cancel</a>
                    </td>
                </tr>
                </table>
                </form:form>
java mysql spring-mvc jpa httpsession
1个回答
0
投票

我在此部分代码中遇到此错误://将乘客更新的数据插入“乘客表”

    RegistredCustomer.setAddress(address);
    RegistredCustomer.setCity(city);
    RegistredCustomer.setPostalCode(postalCode);
    RegistredCustomer.setCountry(country);
© www.soinside.com 2019 - 2024. All rights reserved.