Spring ModelAttribute nullPointerException

728 Views Asked by At

these are my controllers:

@RequestMapping(value="/requestBooking", method = RequestMethod.GET)
@ModelAttribute("booking")
public ModelAndView requestBooking(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ModelAndView view = new ModelAndView("requestBooking");
    OAuthAuthenticationToken token = (OAuthAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    if(token.getUserProfile().getAttributes().get("email").toString().matches(".*@nisum.com")==false){
        SecurityContextHolder.clearContext();
        return new ModelAndView("redirect:/j_spring_security_logout?redirectTo=/rejected");
    }

    VacationBooking booking=new VacationBooking();
    String email=token.getUserProfile().getAttributes().get("email").toString();
    User user=userImp.getUserByMail(email);
    booking.setUser(user); //setting user
    System.out.println(booking.getBookingId() + " - " + booking.getUser().getUserId());
    view.addObject("booking", booking);
    view.addObject("user", token.getUserProfile().getAttributes().get("email"));
    return view;
}




 @RequestMapping(value="/requestBooking/process", method=RequestMethod.POST)
 public ModelAndView addBooking(@ModelAttribute("booking") VacationBooking booking , BindingResult result) {


     /*   val.validate(booking, result);*/

      /*  if(result.hasErrors()){

          System.out.println("Error!!!!");
           ModelAndView modelAndView = new ModelAndView("requestBooking");
           return modelAndView;
        }else{ */




        SimpleMailMessage email2 = new SimpleMailMessage();
        SimpleMailMessage emailPm = new SimpleMailMessage();

        emailPm.setTo(booking.getUser().getMail());   //error here null value
        email2.setTo(booking.getUser().getManager().getMail()); //error here, null value
        email2.setSubject("Vacation Request");
        emailPm.setSubject("Vacation Request");
        emailPm.setText("<h3>Vacation Request<h3>\nThe employee has requested Vacation days between for leave. \n \n Please take action to approve or reject this request. \n \n To review, approve or reject the request, log into SaS, click Vacation Booking and click in Pending Approval link. \n \nNOTE: This is an Automated E-mail generated from the SaS mail process. Please do not reply to this E-mail. Vacation days Request Pending Approval");
        email2.setText("<h3>Vacation Request<h3>\nYour vacation days request has been routed to your manager for approval. \n \nNote: If these dates should change, it is your responsibility to notify the appropriate people or withdraw your request in case you won't take those days.\n \nTo review, withdraw or modify your request, log into SaS, click Vacation Booking and check the current status or your Request. \n \nNOTE: This is an Automated E-mail generated from the SaS mail process. Please do not reply to this E-mail. Vacation days Request Routed for Approval");

        mailSender.send(email2);
        mailSender.send(emailPm);
        System.out.println(booking.getFromDate());
        bookingService.saveVacationBooking(booking);
        ModelAndView modelAndView = new ModelAndView("myBookings");
        String message = "the vacation booking was added!";
        modelAndView.addObject("message", message);
        return modelAndView;

    }

and this is my view:

<!doctype html>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html class="no-js" lang="en">
<head>
    <title>Vacation booking</title>
    <link href="/vacation/resources/bootstrap-3.0.3/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<!--Booking Form-->
<body>
<jsp:include page="../../templates/PageHeader.jsp" />
   <form:form  method="POST" style="width:800px;" commandName="booking"  action="${pageContext.request.contextPath}/requestBooking/process" class="form-horizontal">
<div class="container">
    <div class="row">
        <div clas="col-md-12"><h4>Vacation Booking Form</h4></div>
        <hr>
        <form class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-2 control-label">From:</label>
                <div class="col-sm-6">
                   <form:input type="date" path="fromDate" class="form-control"></form:input>
                   <form:hidden path="user.userId" class="form-control"/>
                   <form:hidden path="bookingId" class="form-control"/>

            <!--<input type="date" class="form-control"/> -->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">To:</label>
                <div class="col-sm-6">
                     <form:input type="date" path="toDate" class="form-control"></form:input>
                    <!-- <input type="date" class="form-control"/> -->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Comments:</label>
                <div class="col-sm-6">
                    <form:textarea path="comments" rows="4" class="form-control" placeholder="Fill this field if you have any comments related to your request"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-lg-offset-2 col-sm-6">
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    </div>
</div>

</form:form>

<jsp:include page="../../templates/PageFooter.jsp" />

<script src="/vacation/resources/js/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="/vacation/resources/bootstrap-3.0.3/js/bootstrap.min.js" type="text/javascript"></script>
</body>
</html>

When I try to do booking.getUser().getMail() I get null. It is like my "booking" object is null in my post method. Any idea of how I can get my booking with the user object inside in my post controller, so I can call the getUser() method?

0

There are 0 best solutions below