Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:57:00+00:00 2026-06-16T16:57:00+00:00

I am using Spring to populate a item in a form, using object passing

  • 0

I am using Spring to populate a item in a form, using object passing between controller and views:

Domain Objects

public class Client {
private String name;
private int id;
private Bar bar;
// getters and setters omitted
}


public class Bar {
private Integer id;
private String name;
// getters and setters omitted

}

Controller

@RequestMapping(method = RequestMethod.GET, value = "/")
public String index(Model m) {
m.addAttribute("Client", new Client());
Bar o1 = new Bar();
o1.setName("n 1");
o1.setId(1);
Bar o2 = new Bar();
o2.setName("n 2");
o2.setId(2);
Bar o3 = new Bar();
o3.setName("n 3");
o3.setId(3);
Bar o4 = new Bar();
o4.setName("n 4");
o4.setId(4);
Bar o5 = new Bar();
o5.setName("n 5");
o5.setId(5);
List<Bar> orders = new ArrayList<Bar>();
orders.add(o1);
orders.add(o2);
orders.add(o3);
orders.add(o4);
orders.add(o5);
m.addAttribute("ordersList", orders);
return "index";
}

@RequestMapping(method = RequestMethod.POST, value = "/")
public String indexPost(@ModelAttribute("Client") Client g, BindingResult r) {
log.info(g.getId() + " " + g.getName());
log.info(g.getBar().getName());
return "index";
}

View (index.jsp)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form:form method="POST" modelAttribute="Client">
<form:label path="name">Name</form:label>
<form:input path="name" />
<form:label path="id">Id</form:label>
<form:input path="id" />
<form:label path="orders">Order</form:label>
<form:select path="bar" items="${ordersList}" />    
<form:button>Insert</form:button>
</form:form>
</body>
</html>

Problem

When I fill out the form, and make the submit, the controller throw a NullPointerException on log.info(g.getBar().getName());
I assume it is because getBar() returns null.
Where am I wrong with this data binding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:57:01+00:00Added an answer on June 16, 2026 at 4:57 pm

    You are setting a new Client object to the model, but that object is empty. Try first to create the full Client object and then set it to the model in the index() method.

    EDIT:
    So you need a property editor for that field. Adjust and add the method below to your controller:

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
    
        binder.registerCustomEditor(Bar.class, "bar", new PropertyEditorSupport(Bar.class) {
            @Override
            public String getAsText() {
                if(null != getValue()) {
                    return ((Bar) getValue()).getId();
                } else {
                    return StringUtils.EMPTY;
                }
            }
            @Override
            public void setAsText(String text) {
                if(!StringUtils.isEmpty(text)) {
                    setValue(UtilityClassUtils.getBarById(text));
                }
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Spring 2.5 tag library, I have an Integer value in a command form
Using Spring 3 I have two forms: adding Item and invalidating Item I have
I have a : Client Class ListView TextField I need to populate my ListView
When using spring security, specifically with @notation; what is the proper way to access
I am using spring/hibernate stand alone application. if i dont configure Transactions i am
I am using spring mvc in my application,when i created user i have to
Hi I am using spring jdbctemplate to invoke an oracle function which takes an
i am using spring 3.1.0.RELEASE , and my servlet container is tomcat 7 and
I am using Spring MVC 3.1.x with Tiles 2.2.2 (bootstrapped project with Roo) and
I am using Spring MVC with Hibernate. My page has a button that creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.