title: Spring MVC | Could not instantiate property type to auto-grow nested property path
body: <p>Folks,</p>
  
  <p>I'm using Spring MVC (4.1.0.RELEASE) and I have this form that I want to display to the user.
  Here is a snippet for the Thymeleaf view:</p>
  
  <pre><code>&lt;form class="form-login" th:action="@{/admin/question/add}" th:object="${question}" method="post"&gt;
      &lt;div class="login-wrap"&gt;
          &lt;input type="text" class="form-control" placeholder="Question"
                 th:field="*{questionStr}" autofocus&gt; &lt;br&gt;
          &lt;input type="text" class="form-control" placeholder="Option One"
                 th:field="*{answerOptions.optionOne}" autofocus&gt; &lt;br&gt;
          &lt;input type="text" class="form-control" placeholder="Option Two"
                 th:field="*{answerOptions.optionTwo}" autofocus&gt; &lt;br&gt;
          &lt;input type="text" class="form-control" placeholder="Option Three"
                 th:field="*{answerOptions.optionThree}" autofocus&gt; &lt;br&gt;
          &lt;input type="text" class="form-control" placeholder="Option Four"
                 th:field="*{answerOptions.optionFour}" autofocus&gt; &lt;br&gt;
          &lt;button class="btn btn-theme btn-block" type="submit"&gt;
              &lt;i class="fa fa-lock"&gt;&lt;/i&gt; Submit
          &lt;/button&gt;
          &lt;hr&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I have exposed <code>question</code> as a form backing object. </p>
  
  <pre><code>@Autowired
  private Question question;
  
  @RequestMapping(value = "add", method = RequestMethod.GET)
  public ModelAndView add(ModelAndView model) {
      model.setViewName("admin/add/question");
      model.addObject("question", question);
      return model;
  }
  </code></pre>
  
  <p>Question Interface:</p>
  
  <pre><code>@Component
  public interface Question {
  
      String getQuestionStr();
  
      void setQuestionStr(String question);
  
      ....
  }
  </code></pre>
  
  <p>Question Implementation:</p>
  
  <pre><code>@Component
  public class QuestionImpl implements Question {
  
      private String questionStr;
  
      private Answer answerOptions;
  
      private Answer correctAnswer;
  
      private Set&lt;TagsImpl&gt; tags;
  
      ....
  }
  </code></pre>
  
  <p>Similarly, I have Answer interface and AnswerImpl.</p>
  
  <p>Now the problem I face when I request for this page is I get the following exception:</p>
  
  <pre><code>org.springframework.beans.NullValueInNestedPathException: Invalid property 'answerOptions' of bean class [com.xyz.abc.bean.impl.QuestionImpl]: Could not instantiate property type [com.saxena.vaibhav.bean.Answer] to auto-grow nested property path: java.lang.InstantiationException: com.xyz.abc.bean.Answer
  </code></pre>
  
  <p>I understand it is not able to instantiate any interfaces. So I changes replaced Answer by AnswerImpl in Question.java and QuestionImpl.java. This solved the problem. But it does not sound like a good solution having concrete implementations in interfaces. </p>
  
  <p>Is there a way I can get around this error?</p>
  
  <p>Spring Configuration:</p>
  
  <pre><code>@Configuration
  @ComponentScan("com.xyz.abc")
  @EnableWebMvc
  public class WebConfig extends WebMvcConfigurerAdapter {
  
      /**
       * 
       * @return ServletContextTemplateResolver ServletContextTemplateResolver.
       */
      @Bean
      @Description("Thymeleaf template resolver for serving HTML 5")
      public ServletContextTemplateResolver templateResolver() {
          ServletContextTemplateResolver templateresolver = new ServletContextTemplateResolver();
          templateresolver.setPrefix("/WEB-INF/views/");
          templateresolver.setSuffix(".html");
          templateresolver.setTemplateMode("LEGACYHTML5");
          templateresolver.setCacheable(false);
          return templateresolver;
      }
  
      @Bean
      @Description("Thymeleaf Template Engine with Spring Integration")
      public SpringTemplateEngine templateEngine() {
          SpringTemplateEngine templateEngine = new SpringTemplateEngine();
          templateEngine.setTemplateResolver(templateResolver());
          return templateEngine;
      }
  
      @Bean
      @Description("Thymeleaf View Resolver")
      public ThymeleafViewResolver thymeleafViewResolver(){
          ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
          viewResolver.setTemplateEngine(templateEngine());
          return viewResolver;
      }
  
      @Override   
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
      }
  
      @Bean(name = "hibernateProperties")
      public PropertiesFactoryBean hibernateProperties() {
  
          PropertiesFactoryBean bean = new PropertiesFactoryBean();
          bean.setLocation(new ClassPathResource("properties/hibernate.props"));
          return bean;
      }
  }
  </code></pre>
  
  <p>Answer.java</p>
  
  <pre><code>@Component
  public interface Answer {
  
       String getOptionOne();
  
       void setOptionOne(String optionOne);
  
       String getOptionTwo();
  
       void setOptionTwo(String optionTwo);
  
       String getOptionThree();
  
       void setOptionThree(String optionThree);
  
       String getOptionFour();
  
       void setOptionFour(String optionFour);
  }
  </code></pre>