2

I'm beginning JSP. I have the following HTML form.

<form method='POST' enctype='multipart/form-data'>
    <input type="text" name="sittingPlaces">
    <textarea name="invitees"></textarea>
    <input type="submit" value="Submit">
</form>

And the following java code.

if (request != null && request.getContentType() != null) {
    int sittingPlaces = Integer.parseInt(request.getParameter("sittingPlaces"));
    String invites = request.getParameter("invitees");
}

I get an error at

int sittingPlaces = Integer.parseInt(request.getParameter("sittingPlaces"));

Any idea why? Thanks loads.

LPB
  • 515
  • 4
  • 12
  • 20
  • **Exception:** org.apache.jasper.JasperException: An exception occurred processing JSP page /TP2.jsp at line 41 **root cause:** java.lang.NumberFormatException: null – LPB Feb 05 '15 at 03:50
  • 1
    NumberFormatException: "Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format." which value are you assigning to sittingPlaces? – Leandro Carracedo Feb 05 '15 at 03:52
  • I tried leaving the text field empty, and tried writing a number in it as well. – LPB Feb 05 '15 at 03:54
  • 1
    You should validate that `request.getParameter("sittingPlaces")` is not null – Ascalonian Feb 05 '15 at 03:56

2 Answers2

5

Check if a string request.getParameter("sittingPlaces") is a valid number with the following method:

public boolean isInteger(String str) {
    try {
        Integer.parseInt(str);
    } catch (NumberFormatException e) {
        return false; // The string isn't a valid number
    }
    return true; // The string is a valid number
}

Or you can implement it inside your code:

if (request != null && request.getContentType() != null) {
    String sittingPlacesStr = request.getParameter("sittingPlaces");
    try {
        int sittingPlaces = Integer.parseInt(sittingPlacesStr);
        String invites = request.getParameter("invitees");
    } catch (NumberFormatException | NullPointerException e) {
        // handle the error here
    }
}

The problem you are facing is that NumberFormatException is thrown because Java fails to convert your String to Integer because it does not represent a valid Integer. You should use a try-catch statement (just like in the example method above), to filter that Exception, as you have no control over the request coming from the client.

Additionaly:

You should also check if the request.getParameter("sittingPlaces") expression returns a valid string, and not a null: String sittingPlaces = request.getParameter("sittingPlaces");

if (sittingPlaces != null {
    // Continue your code here
} else {
    // The client request did not provide the parameter "sittingPlaces"
}
Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • Thanks. Worked like a charm. Another thing that I realized is that I am using post method and therefore couldn't get the parameters as they are searched in the body, not in the URL. Can you confirm this? – LPB Feb 05 '15 at 04:17
  • @Louis-PhilippeBaillargeon The `request.getParameter(...)` method should always get the parameter from the request with the specified name, regardless of whether it is a **GET** method or a **POST** method. check if your HTML form is submitting the right parameters, with the right names. – Victor2748 Feb 05 '15 at 04:52
  • @Louis-PhilippeBaillargeon Also, what do you mean "Couldn't get the parameters"? Do they return `null`, or trow an exception, or do anything else? – Victor2748 Feb 05 '15 at 04:54
  • They are returning null. So this normal when using **POST**? – LPB Feb 05 '15 at 04:59
  • @Louis-PhilippeBaillargeon No, Try changing the method type from POST to GET, and repeat the operation again. I am sure that you will get the same result. Also, check what method (function) are you running the code in? `doGet()`, `doPost()`, or anything else? – Victor2748 Feb 05 '15 at 05:55
2

Check the value you are getting in sittingPlaces request parameter. Just try to print that on console using

System.out.println(request.getParameter("sittingPlaces")); 

and see the output.any trailing spaces, alphabet or special characters present.

In this case I believe you might be passing characters or trailing spaces.