开发者

Nullpointer exception in doPost

开发者 https://www.devze.com 2023-03-28 01:32 出处:网络
I have de following Form: <form action=\"/AppStore/publish\" method=\"post\" accept-charset=\"ISO-8859-1\">

I have de following Form:

        <form action="/AppStore/publish" method="post" accept-charset="ISO-8859-1">
        <fieldset>
            <legend>Do you have an Account already?</legend>
                <input type="radio" name="registred" value="yes"> Yes
                <input type="radio" name="registred" value="no"> No
        </fieldset>
        <fieldset>
            <legend>About your App</legend>
            <table>
                <tr>
                    <td><label for="AppDesc">Describe it:</label></td>
                    <td><input type="text" name="AppDesc" /></td>
                </tr>

                <tr>
                    <td><label for="AppName">Name:</label></td> 
                    <td><input type="text" name="AppName" /></td>
                </tr>
            </table>    
        </fieldset>
        <input type="submit" value="Submit" />
    </form>

I pass this data to a Java Servlet, but every time I get a Nullpointer Exception at getParameter("AppDesc"), instead getParameter("AppName") works fine, what do I wrong?

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletContext context = getServletContext();
    RequestDispatcher dispetcher = context.getRequestDispatcher("/publishForm.jsp");
    List<String> errorMessages = new ArrayList<String>();

    //Validating form input...

    if(request.getParameter("AppName").toString().isEmpty())
    {
        errorMessages.add("Please type a valid Name for your App.");
    }

  开发者_Python百科  if(request.getParameter("AppDesc").toString().isEmpty())
    {
        errorMessages.add("The Description of your App should contain at least 160 Characters.");
    }


You're calling request.getParameter("...").toString().

request.getParameter() already returns a string reference, so you don't actually need to call toString() to get the value as a string, but it will return a null reference if the parameter is missing - in which case calling toString() will throw an exception. You need to check whether the value is null or empty. For example:

String description = request.getParameter("AppDesc");
if (description == null || description.isEmpty())
...

Of course, there are libraries around to check for "null or empty" - for example, in Guava you could use:

if (Strings.isNullOrEmpty(description))


If request.getParameter("AppDesc") is null, then

request.getParameter("AppDesc").toString().isEmpty() will throw a NullPointerException.

Why not change the condition to:

   if(request.getParameter("AppDesc") == null ||   
      request.getParameter("AppDesc").toString().isEmpty()))
   {


<td><input type="text" name="AppDescr" /></td>

You've named the actual field AppDescr (notice the trailing "r"), but you're calling getParameter for AppDesc (sans "r").

EDIT: Or not... you edited your post and fixed it. Was this not the problem?


It must be the case that request.getParameter("AppDesc") returns a null value, causing the chained toString() to generate a NullPointerException.

This parameter was never set; the name specified in the html was "AppDesr" (note the trailing 'r').


Your question title says doGet, your code says doPost. The difference between those two might explain your problem. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消