servlet accept null values from checkboxes The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Sort a Map<Key, Value> by valuesCreate ArrayList from arrayAvoiding != null statementsHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?What is the difference between JSF, Servlet and JSP?How to upload files to server using JSP/Servlet?How do servlets work? Instantiation, sessions, shared variables and multithreading

It is correct to match light sources with the same color temperature?

Reshaping json / reparing json inside shell script (remove trailing comma)

Point distance program written without a framework

Players Circumventing the limitations of Wish

TikZ: How to fill area with a special pattern?

Yu-Gi-Oh cards in Python 3

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

Cannot shrink btrfs filesystem although there is still data and metadata space left : ERROR: unable to resize '/home': No space left on device

From jafe to El-Guest

Do scriptures give a method to recognize a truly self-realized person/jivanmukta?

Where do students learn to solve polynomial equations these days?

Is a distribution that is normal, but highly skewed, considered Gaussian?

how one can write a nice vector parser, something that does pgfvecparseA=B-C; D=E x F;

What is the difference between "hamstring tendon" and "common hamstring tendon"?

Physiological effects of huge anime eyes

What would be the main consequences for a country leaving the WTO?

Is there a reasonable and studied concept of reduction between regular languages?

Is it convenient to ask the journal's editor for two additional days to complete a review?

Towers in the ocean; How deep can they be built?

What is the process for purifying your home if you believe it may have been previously used for pagan worship?

How did Beeri the Hittite come up with naming his daughter Yehudit?

Purpose of level-shifter with same in and out voltages

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what chip

Which Pokemon have a special animation when running with them out of their pokeball?



servlet accept null values from checkboxes



The Next CEO of Stack OverflowIs Java “pass-by-reference” or “pass-by-value”?Sort a Map<Key, Value> by valuesCreate ArrayList from arrayAvoiding != null statementsHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?What is the difference between JSF, Servlet and JSP?How to upload files to server using JSP/Servlet?How do servlets work? Instantiation, sessions, shared variables and multithreading










1















enter image description herehere I have a jsp page which allow the user to apply his name, his age and his mastered programming language



<form action="./getPost" method="POST" >

<label> name </label> <input type="text" name="name" > <br><br>
<label> age </label> &nbsp <input type="text" name="age" > <br><br>


<label> programming langage </label> <br>
<input type="checkbox" name="fav" value="java"> java <br>
<input type="checkbox" name="fav" value="php"> php <br>
<input type="checkbox" name="fav" value="python"> python <br><br>

<input type="submit" value="submit post">

</form>


this is how the servlet get the data



 String name = request.getParameter("name");
String age = request.getParameter("age");
String[] lang = request.getParameterValues("fav");

InsertPost.add(new Post(name, age, lang));


when I fill in all the fields of the form and click on the button it inserts the data into the database.
then I tested to submit it without selecting any checkbox which caused an error (java.lang.NullPointerException).
the issue is that I want to leave this optional means not required if the user wants to pick or not.
so how can I make the servlet handles null values ​​from checkboxes










share|improve this question
























  • help us with full logs here...

    – vancleff
    Mar 7 at 18:32











  • you mean what does the server displyed ?

    – user11114695
    Mar 7 at 18:34











  • yes the error that you got on server?

    – vancleff
    Mar 7 at 18:35











  • I've added it, it's in french by the way..

    – user11114695
    Mar 7 at 18:38











  • I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

    – vancleff
    Mar 7 at 19:07















1















enter image description herehere I have a jsp page which allow the user to apply his name, his age and his mastered programming language



<form action="./getPost" method="POST" >

<label> name </label> <input type="text" name="name" > <br><br>
<label> age </label> &nbsp <input type="text" name="age" > <br><br>


<label> programming langage </label> <br>
<input type="checkbox" name="fav" value="java"> java <br>
<input type="checkbox" name="fav" value="php"> php <br>
<input type="checkbox" name="fav" value="python"> python <br><br>

<input type="submit" value="submit post">

</form>


this is how the servlet get the data



 String name = request.getParameter("name");
String age = request.getParameter("age");
String[] lang = request.getParameterValues("fav");

InsertPost.add(new Post(name, age, lang));


when I fill in all the fields of the form and click on the button it inserts the data into the database.
then I tested to submit it without selecting any checkbox which caused an error (java.lang.NullPointerException).
the issue is that I want to leave this optional means not required if the user wants to pick or not.
so how can I make the servlet handles null values ​​from checkboxes










share|improve this question
























  • help us with full logs here...

    – vancleff
    Mar 7 at 18:32











  • you mean what does the server displyed ?

    – user11114695
    Mar 7 at 18:34











  • yes the error that you got on server?

    – vancleff
    Mar 7 at 18:35











  • I've added it, it's in french by the way..

    – user11114695
    Mar 7 at 18:38











  • I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

    – vancleff
    Mar 7 at 19:07













1












1








1








enter image description herehere I have a jsp page which allow the user to apply his name, his age and his mastered programming language



<form action="./getPost" method="POST" >

<label> name </label> <input type="text" name="name" > <br><br>
<label> age </label> &nbsp <input type="text" name="age" > <br><br>


<label> programming langage </label> <br>
<input type="checkbox" name="fav" value="java"> java <br>
<input type="checkbox" name="fav" value="php"> php <br>
<input type="checkbox" name="fav" value="python"> python <br><br>

<input type="submit" value="submit post">

</form>


this is how the servlet get the data



 String name = request.getParameter("name");
String age = request.getParameter("age");
String[] lang = request.getParameterValues("fav");

InsertPost.add(new Post(name, age, lang));


when I fill in all the fields of the form and click on the button it inserts the data into the database.
then I tested to submit it without selecting any checkbox which caused an error (java.lang.NullPointerException).
the issue is that I want to leave this optional means not required if the user wants to pick or not.
so how can I make the servlet handles null values ​​from checkboxes










share|improve this question
















enter image description herehere I have a jsp page which allow the user to apply his name, his age and his mastered programming language



<form action="./getPost" method="POST" >

<label> name </label> <input type="text" name="name" > <br><br>
<label> age </label> &nbsp <input type="text" name="age" > <br><br>


<label> programming langage </label> <br>
<input type="checkbox" name="fav" value="java"> java <br>
<input type="checkbox" name="fav" value="php"> php <br>
<input type="checkbox" name="fav" value="python"> python <br><br>

<input type="submit" value="submit post">

</form>


this is how the servlet get the data



 String name = request.getParameter("name");
String age = request.getParameter("age");
String[] lang = request.getParameterValues("fav");

InsertPost.add(new Post(name, age, lang));


when I fill in all the fields of the form and click on the button it inserts the data into the database.
then I tested to submit it without selecting any checkbox which caused an error (java.lang.NullPointerException).
the issue is that I want to leave this optional means not required if the user wants to pick or not.
so how can I make the servlet handles null values ​​from checkboxes







java jsp servlets java-ee java-ee-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 18:55

























asked Mar 7 at 18:29







user11114695



















  • help us with full logs here...

    – vancleff
    Mar 7 at 18:32











  • you mean what does the server displyed ?

    – user11114695
    Mar 7 at 18:34











  • yes the error that you got on server?

    – vancleff
    Mar 7 at 18:35











  • I've added it, it's in french by the way..

    – user11114695
    Mar 7 at 18:38











  • I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

    – vancleff
    Mar 7 at 19:07

















  • help us with full logs here...

    – vancleff
    Mar 7 at 18:32











  • you mean what does the server displyed ?

    – user11114695
    Mar 7 at 18:34











  • yes the error that you got on server?

    – vancleff
    Mar 7 at 18:35











  • I've added it, it's in french by the way..

    – user11114695
    Mar 7 at 18:38











  • I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

    – vancleff
    Mar 7 at 19:07
















help us with full logs here...

– vancleff
Mar 7 at 18:32





help us with full logs here...

– vancleff
Mar 7 at 18:32













you mean what does the server displyed ?

– user11114695
Mar 7 at 18:34





you mean what does the server displyed ?

– user11114695
Mar 7 at 18:34













yes the error that you got on server?

– vancleff
Mar 7 at 18:35





yes the error that you got on server?

– vancleff
Mar 7 at 18:35













I've added it, it's in french by the way..

– user11114695
Mar 7 at 18:38





I've added it, it's in french by the way..

– user11114695
Mar 7 at 18:38













I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

– vancleff
Mar 7 at 19:07





I am assuming you just expect null value for fav not for name and age? Did you check if you have any null constraint on the fav field?

– vancleff
Mar 7 at 19:07












2 Answers
2






active

oldest

votes


















0














How to solve this issue:



Option 1: (sending empty string instead of null)
Assuming that your table has a not-null constraint on field fav.



String name = request.getParameter("name");
String age = request.getParameter("age");
List<String> lang = request.getParameterValues("fav");
if(lang==null)
lang = new String[] "";

InsertPost.add(new Post(name, age, lang));
//not recommended as it will add 1-byte of empty string even if there is no fav language


Note:




You should put the InsertPost.add(...) call in a try-catch block to
handle exception during inserting data to DB and then show it on UI as a part of best
practices. Also, put a check if the name and age are null and handle it.




Option 2: (sending null and removing null constraint from the table on fav field)



//how you are doing it previously will work.
InsertPost.add(new Post(name, age, lang));


Cause of error




Your database is not expecting null value on filed favorite languages






share|improve this answer

























  • yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

    – user11114695
    Mar 7 at 19:19











  • Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

    – vancleff
    Mar 7 at 19:20











  • hahà yes, I did

    – user11114695
    Mar 7 at 19:23


















0














//One suggestion: treat null and replace with empty String



 String name = (request.getParameter("name") == null ? "": request.getParameter("name"));
String age = (request.getParameter("age") == null) ? "" : request.getParameter("age"));
String[] lang = (request.getParameterValues("fav") == null) ? "": request.getParameterValues("fav"));


I hope it help you.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050569%2fservlet-accept-null-values-from-checkboxes%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    How to solve this issue:



    Option 1: (sending empty string instead of null)
    Assuming that your table has a not-null constraint on field fav.



    String name = request.getParameter("name");
    String age = request.getParameter("age");
    List<String> lang = request.getParameterValues("fav");
    if(lang==null)
    lang = new String[] "";

    InsertPost.add(new Post(name, age, lang));
    //not recommended as it will add 1-byte of empty string even if there is no fav language


    Note:




    You should put the InsertPost.add(...) call in a try-catch block to
    handle exception during inserting data to DB and then show it on UI as a part of best
    practices. Also, put a check if the name and age are null and handle it.




    Option 2: (sending null and removing null constraint from the table on fav field)



    //how you are doing it previously will work.
    InsertPost.add(new Post(name, age, lang));


    Cause of error




    Your database is not expecting null value on filed favorite languages






    share|improve this answer

























    • yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

      – user11114695
      Mar 7 at 19:19











    • Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

      – vancleff
      Mar 7 at 19:20











    • hahà yes, I did

      – user11114695
      Mar 7 at 19:23















    0














    How to solve this issue:



    Option 1: (sending empty string instead of null)
    Assuming that your table has a not-null constraint on field fav.



    String name = request.getParameter("name");
    String age = request.getParameter("age");
    List<String> lang = request.getParameterValues("fav");
    if(lang==null)
    lang = new String[] "";

    InsertPost.add(new Post(name, age, lang));
    //not recommended as it will add 1-byte of empty string even if there is no fav language


    Note:




    You should put the InsertPost.add(...) call in a try-catch block to
    handle exception during inserting data to DB and then show it on UI as a part of best
    practices. Also, put a check if the name and age are null and handle it.




    Option 2: (sending null and removing null constraint from the table on fav field)



    //how you are doing it previously will work.
    InsertPost.add(new Post(name, age, lang));


    Cause of error




    Your database is not expecting null value on filed favorite languages






    share|improve this answer

























    • yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

      – user11114695
      Mar 7 at 19:19











    • Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

      – vancleff
      Mar 7 at 19:20











    • hahà yes, I did

      – user11114695
      Mar 7 at 19:23













    0












    0








    0







    How to solve this issue:



    Option 1: (sending empty string instead of null)
    Assuming that your table has a not-null constraint on field fav.



    String name = request.getParameter("name");
    String age = request.getParameter("age");
    List<String> lang = request.getParameterValues("fav");
    if(lang==null)
    lang = new String[] "";

    InsertPost.add(new Post(name, age, lang));
    //not recommended as it will add 1-byte of empty string even if there is no fav language


    Note:




    You should put the InsertPost.add(...) call in a try-catch block to
    handle exception during inserting data to DB and then show it on UI as a part of best
    practices. Also, put a check if the name and age are null and handle it.




    Option 2: (sending null and removing null constraint from the table on fav field)



    //how you are doing it previously will work.
    InsertPost.add(new Post(name, age, lang));


    Cause of error




    Your database is not expecting null value on filed favorite languages






    share|improve this answer















    How to solve this issue:



    Option 1: (sending empty string instead of null)
    Assuming that your table has a not-null constraint on field fav.



    String name = request.getParameter("name");
    String age = request.getParameter("age");
    List<String> lang = request.getParameterValues("fav");
    if(lang==null)
    lang = new String[] "";

    InsertPost.add(new Post(name, age, lang));
    //not recommended as it will add 1-byte of empty string even if there is no fav language


    Note:




    You should put the InsertPost.add(...) call in a try-catch block to
    handle exception during inserting data to DB and then show it on UI as a part of best
    practices. Also, put a check if the name and age are null and handle it.




    Option 2: (sending null and removing null constraint from the table on fav field)



    //how you are doing it previously will work.
    InsertPost.add(new Post(name, age, lang));


    Cause of error




    Your database is not expecting null value on filed favorite languages







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 19:10

























    answered Mar 7 at 19:05









    vancleffvancleff

    40539




    40539












    • yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

      – user11114695
      Mar 7 at 19:19











    • Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

      – vancleff
      Mar 7 at 19:20











    • hahà yes, I did

      – user11114695
      Mar 7 at 19:23

















    • yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

      – user11114695
      Mar 7 at 19:19











    • Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

      – vancleff
      Mar 7 at 19:20











    • hahà yes, I did

      – user11114695
      Mar 7 at 19:23
















    yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

    – user11114695
    Mar 7 at 19:19





    yes the "InsertPost.add(...)"is already surrounded by try-catch block I just didn't print it here in order to only show the code. and in my table I haven't indicated the constraint NOT NULL

    – user11114695
    Mar 7 at 19:19













    Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

    – vancleff
    Mar 7 at 19:20





    Awesome, I think you've read the guidelines for posting a question thoroughly, highly appreciated. :)

    – vancleff
    Mar 7 at 19:20













    hahà yes, I did

    – user11114695
    Mar 7 at 19:23





    hahà yes, I did

    – user11114695
    Mar 7 at 19:23













    0














    //One suggestion: treat null and replace with empty String



     String name = (request.getParameter("name") == null ? "": request.getParameter("name"));
    String age = (request.getParameter("age") == null) ? "" : request.getParameter("age"));
    String[] lang = (request.getParameterValues("fav") == null) ? "": request.getParameterValues("fav"));


    I hope it help you.






    share|improve this answer





























      0














      //One suggestion: treat null and replace with empty String



       String name = (request.getParameter("name") == null ? "": request.getParameter("name"));
      String age = (request.getParameter("age") == null) ? "" : request.getParameter("age"));
      String[] lang = (request.getParameterValues("fav") == null) ? "": request.getParameterValues("fav"));


      I hope it help you.






      share|improve this answer



























        0












        0








        0







        //One suggestion: treat null and replace with empty String



         String name = (request.getParameter("name") == null ? "": request.getParameter("name"));
        String age = (request.getParameter("age") == null) ? "" : request.getParameter("age"));
        String[] lang = (request.getParameterValues("fav") == null) ? "": request.getParameterValues("fav"));


        I hope it help you.






        share|improve this answer















        //One suggestion: treat null and replace with empty String



         String name = (request.getParameter("name") == null ? "": request.getParameter("name"));
        String age = (request.getParameter("age") == null) ? "" : request.getParameter("age"));
        String[] lang = (request.getParameterValues("fav") == null) ? "": request.getParameterValues("fav"));


        I hope it help you.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 7 at 21:00









        Dr Mido

        737631




        737631










        answered Mar 7 at 19:05









        Paulo LimaPaulo Lima

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050569%2fservlet-accept-null-values-from-checkboxes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved