Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC | Could not instantiate property type to auto-grow nested property path

Folks,

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:

<form class="form-login" th:action="@{/admin/question/add}" th:object="${question}" method="post">
    <div class="login-wrap">
        <input type="text" class="form-control" placeholder="Question"
               th:field="*{questionStr}" autofocus> <br>
        <input type="text" class="form-control" placeholder="Option One"
               th:field="*{answerOptions.optionOne}" autofocus> <br>
        <input type="text" class="form-control" placeholder="Option Two"
               th:field="*{answerOptions.optionTwo}" autofocus> <br>
        <input type="text" class="form-control" placeholder="Option Three"
               th:field="*{answerOptions.optionThree}" autofocus> <br>
        <input type="text" class="form-control" placeholder="Option Four"
               th:field="*{answerOptions.optionFour}" autofocus> <br>
        <button class="btn btn-theme btn-block" type="submit">
            <i class="fa fa-lock"></i> Submit
        </button>
        <hr>
    </div>
</form>

I have exposed question as a form backing object.

@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;
}

Question Interface:

@Component
public interface Question {

    String getQuestionStr();

    void setQuestionStr(String question);

    ....
}

Question Implementation:

@Component
public class QuestionImpl implements Question {

    private String questionStr;

    private Answer answerOptions;

    private Answer correctAnswer;

    private Set<TagsImpl> tags;

    ....
}

Similarly, I have Answer interface and AnswerImpl.

Now the problem I face when I request for this page is I get the following exception:

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

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.

Is there a way I can get around this error?

Spring Configuration:

@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;
    }
}

Answer.java

@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);
}
like image 813
va1b4av Avatar asked Oct 23 '25 16:10

va1b4av


1 Answers

I had the similar issue and I was able to fix it by adding empty public constructor to Answer and TagsImpl

public UserEntity(){}

public TagsImpl(){}
like image 94
tk_ Avatar answered Oct 26 '25 07:10

tk_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!