1

I am trying to read a datetime value from a JSP form in my servlet:

ConcertController:

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String action = request.getParameter("action");
    if ("add_concert".equals(action)) {
        Concert concert = new Concert();
        ...
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        java.util.Date parsed = new java.util.Date();
        try {
            parsed = format.parse(request.getParameter("concert_datetime"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
        concert.setConcertTime(sqlDate);
        ..
        ));
        concert.setTicketprice(Double.parseDouble(request.getParameter("concert_ticketprice")));

        ConcertTable.insertConcert(concert);

        request.getRequestDispatcher("ConcertsController?action=get_all_concerts").forward(request, response);
    }
}

.jsp:

  <form action="ConcertController?action=add_concert" method="post"
            name="concertAddForm" id="formAddConcert"
            enctype="multipart/form-data">
            <h2>Add new concert</h2>
            <div class="panel panel-success">
                <div class="col-md-6 form-group">
                    <label>Concert name:</label> <input type="text" name="concert_name"
                        placeholder="Concert Name" class="form-control" required>
                </div>
                ...
                <div class="col-md-6 form-group">
                    <label>Date / Time:</label> <input type="text"
                        name="concert_datetime" placeholder="dd/MM/yyyy HH:mm"
                        class="form-control" required>
                </div>              
            </div>
            <br> <input type="submit" id="addConcertBtn"
                class="btn btn-primary btn-large" value="Add concert">
        </form>

but I keep getting an HTTP status 500:

java.lang.NullPointerException java.text.SimpleDateFormat.parse(Unknown Source) java.text.DateFormat.parse(Unknown Source)

I tried with 12/12/2000 20:00.

What am I doing wrong?

6
  • have you tried input type="date" Commented Dec 10, 2017 at 17:25
  • @SinghPiyush Yes, and "datetime" - no difference. Commented Dec 10, 2017 at 17:28
  • 1
    request.getParameter("concert_datetime") returns null. There might be some problems with the form that you post or with how you post it. Now there are not enough details. Commented Dec 10, 2017 at 17:30
  • 2
    Why do you use enctype="multipart/form-data"? As I can see you don't send any multipart content/files etc. If you remove it, the code should work. Otherwise, handling will be more complex. Commented Dec 10, 2017 at 17:50
  • 1
    add @MultipartConfig top on your Servlet class because of enctype="multipart/form-data" Commented Dec 10, 2017 at 17:51

1 Answer 1

1

Eutherpy, if you don't need to upload files in your form, just get rid of

enctype="multipart/form-data"

Since it forces your form to be sent in multipart part format.

Otherwise, if you do need to upload files, there were several answers already.

You can have a look at this answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.