Wicket DropDownChoice selected value












1














I am trying to get the Id of my selected choice on a DropDownChoice but I get an error..
I know that when I choose a value I just update the model and not the object (reflection).
I expected to get all the values of object "User" through getModelObject() but all i get is a NullPointerException..
I have tried many things according to tutorials and Wicket 8 documentation but nothing seems to work..



My code is like:



  // POJO
class User {
private Integer id;
private String name;
[...]

}

// Main.class
private User selected;

ChoiceRenderer<User> choiceRenderer = new ChoiceRenderer<User>("id", "name");
List<User> list = getUsers();

final DropDownChoice<User> dropdown1 = new DropDownChoice<User>("dropdown",
new PropertyModel<User>(this, "selected"), list, choiceRenderer);


Button btn = new Button("btn") {

private static final long serialVersionUID = 1L;

@Override
public void onSubmit() {
RecrRemoteOperations recr = new RecrRemoteOperations();
try {

// NullPointerException!
// Integer id = dropdown.getModel().getObject().getId();
// id: the id of the selected "User" value on dropdown
recr.updateCommand(id);

} catch (Throwable e) {
e.printStackTrace();
}
}
}.setDefaultFormProcessing(false);

private static List<User> getUsers() {
List<User> allUsers = new ArrayList<User>();
[...]
return list;
}









share|improve this question
























  • Do you have a getter and a setter for selected?
    – GustavoCinque
    Nov 19 '18 at 16:44










  • Yeah I tried that but the "selected" object is empty..
    – user10376560
    Nov 19 '18 at 16:47
















1














I am trying to get the Id of my selected choice on a DropDownChoice but I get an error..
I know that when I choose a value I just update the model and not the object (reflection).
I expected to get all the values of object "User" through getModelObject() but all i get is a NullPointerException..
I have tried many things according to tutorials and Wicket 8 documentation but nothing seems to work..



My code is like:



  // POJO
class User {
private Integer id;
private String name;
[...]

}

// Main.class
private User selected;

ChoiceRenderer<User> choiceRenderer = new ChoiceRenderer<User>("id", "name");
List<User> list = getUsers();

final DropDownChoice<User> dropdown1 = new DropDownChoice<User>("dropdown",
new PropertyModel<User>(this, "selected"), list, choiceRenderer);


Button btn = new Button("btn") {

private static final long serialVersionUID = 1L;

@Override
public void onSubmit() {
RecrRemoteOperations recr = new RecrRemoteOperations();
try {

// NullPointerException!
// Integer id = dropdown.getModel().getObject().getId();
// id: the id of the selected "User" value on dropdown
recr.updateCommand(id);

} catch (Throwable e) {
e.printStackTrace();
}
}
}.setDefaultFormProcessing(false);

private static List<User> getUsers() {
List<User> allUsers = new ArrayList<User>();
[...]
return list;
}









share|improve this question
























  • Do you have a getter and a setter for selected?
    – GustavoCinque
    Nov 19 '18 at 16:44










  • Yeah I tried that but the "selected" object is empty..
    – user10376560
    Nov 19 '18 at 16:47














1












1








1







I am trying to get the Id of my selected choice on a DropDownChoice but I get an error..
I know that when I choose a value I just update the model and not the object (reflection).
I expected to get all the values of object "User" through getModelObject() but all i get is a NullPointerException..
I have tried many things according to tutorials and Wicket 8 documentation but nothing seems to work..



My code is like:



  // POJO
class User {
private Integer id;
private String name;
[...]

}

// Main.class
private User selected;

ChoiceRenderer<User> choiceRenderer = new ChoiceRenderer<User>("id", "name");
List<User> list = getUsers();

final DropDownChoice<User> dropdown1 = new DropDownChoice<User>("dropdown",
new PropertyModel<User>(this, "selected"), list, choiceRenderer);


Button btn = new Button("btn") {

private static final long serialVersionUID = 1L;

@Override
public void onSubmit() {
RecrRemoteOperations recr = new RecrRemoteOperations();
try {

// NullPointerException!
// Integer id = dropdown.getModel().getObject().getId();
// id: the id of the selected "User" value on dropdown
recr.updateCommand(id);

} catch (Throwable e) {
e.printStackTrace();
}
}
}.setDefaultFormProcessing(false);

private static List<User> getUsers() {
List<User> allUsers = new ArrayList<User>();
[...]
return list;
}









share|improve this question















I am trying to get the Id of my selected choice on a DropDownChoice but I get an error..
I know that when I choose a value I just update the model and not the object (reflection).
I expected to get all the values of object "User" through getModelObject() but all i get is a NullPointerException..
I have tried many things according to tutorials and Wicket 8 documentation but nothing seems to work..



My code is like:



  // POJO
class User {
private Integer id;
private String name;
[...]

}

// Main.class
private User selected;

ChoiceRenderer<User> choiceRenderer = new ChoiceRenderer<User>("id", "name");
List<User> list = getUsers();

final DropDownChoice<User> dropdown1 = new DropDownChoice<User>("dropdown",
new PropertyModel<User>(this, "selected"), list, choiceRenderer);


Button btn = new Button("btn") {

private static final long serialVersionUID = 1L;

@Override
public void onSubmit() {
RecrRemoteOperations recr = new RecrRemoteOperations();
try {

// NullPointerException!
// Integer id = dropdown.getModel().getObject().getId();
// id: the id of the selected "User" value on dropdown
recr.updateCommand(id);

} catch (Throwable e) {
e.printStackTrace();
}
}
}.setDefaultFormProcessing(false);

private static List<User> getUsers() {
List<User> allUsers = new ArrayList<User>();
[...]
return list;
}






java wicket dropdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:51

























asked Nov 19 '18 at 16:38







user10376560



















  • Do you have a getter and a setter for selected?
    – GustavoCinque
    Nov 19 '18 at 16:44










  • Yeah I tried that but the "selected" object is empty..
    – user10376560
    Nov 19 '18 at 16:47


















  • Do you have a getter and a setter for selected?
    – GustavoCinque
    Nov 19 '18 at 16:44










  • Yeah I tried that but the "selected" object is empty..
    – user10376560
    Nov 19 '18 at 16:47
















Do you have a getter and a setter for selected?
– GustavoCinque
Nov 19 '18 at 16:44




Do you have a getter and a setter for selected?
– GustavoCinque
Nov 19 '18 at 16:44












Yeah I tried that but the "selected" object is empty..
– user10376560
Nov 19 '18 at 16:47




Yeah I tried that but the "selected" object is empty..
– user10376560
Nov 19 '18 at 16:47












2 Answers
2






active

oldest

votes


















1














The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.



.setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.






share|improve this answer





















  • Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
    – user10376560
    Nov 20 '18 at 6:54



















0














I am not sure but my problem is very similar to this question
I was told that I don't need to use Ajax but I will try to see if it works






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%2f53379070%2fwicket-dropdownchoice-selected-value%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









    1














    The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.



    .setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.






    share|improve this answer





















    • Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
      – user10376560
      Nov 20 '18 at 6:54
















    1














    The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.



    .setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.






    share|improve this answer





















    • Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
      – user10376560
      Nov 20 '18 at 6:54














    1












    1








    1






    The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.



    .setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.






    share|improve this answer












    The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.



    .setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 6:31









    martin-g

    12k1825




    12k1825












    • Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
      – user10376560
      Nov 20 '18 at 6:54


















    • Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
      – user10376560
      Nov 20 '18 at 6:54
















    Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
    – user10376560
    Nov 20 '18 at 6:54




    Indeed the problem was that function .setDefaultFormProcessing(false).. I copied the DropDownChoice from another page and I didnt pay attention.. :) Thank you martin-g
    – user10376560
    Nov 20 '18 at 6:54













    0














    I am not sure but my problem is very similar to this question
    I was told that I don't need to use Ajax but I will try to see if it works






    share|improve this answer


























      0














      I am not sure but my problem is very similar to this question
      I was told that I don't need to use Ajax but I will try to see if it works






      share|improve this answer
























        0












        0








        0






        I am not sure but my problem is very similar to this question
        I was told that I don't need to use Ajax but I will try to see if it works






        share|improve this answer












        I am not sure but my problem is very similar to this question
        I was told that I don't need to use Ajax but I will try to see if it works







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 19:06







        user10376560





































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53379070%2fwicket-dropdownchoice-selected-value%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith