0

I have the following Spring code, taken from a textbook, that's returning a NotWritablePropertyException. I'm told I need setters for my Song class, which I thought I'd just done, but it's apparently not working. I've edited my code below. Can anybody help? Here is the code.

package com.springinaction.springidol;

    public class Singer implements Performer {
        private String name = "Someone";
        private Song song;

        public Singer() {
        }

        public Singer(String song) {
            this.song.setTitle(song);
        }

        public Singer(String name, String song) {
            this.song = new Song(song);
            this.name = name;
        }

        public Song getSong() {
            return this.song;
        }

        public void setSong(Song song) {
            this.song = song;
        }

        public void perform() throws PerformanceException {
            System.out.println(name + " IS SINGING " + song.getTitle());
        }
    }

Here is my Song class, which appears to be part of the problem.

public class Song {
    private String title;

    public Song(String song) {
        this.title = song;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Here are the exceptions.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taylor' defined in class path resource [com/springinaction/springidol/spring-idol.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.springinaction.springidol.SpringIdolMain.main(SpringIdolMain.java:8)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    ... 14 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
    ... 18 more

4 Answers 4

3

in practice you should have getters and setters for each field unless you are omitting them for a reason.

3
  • I've tried to add some getters and setters above, but it doesn't look like it worked.
    – user636859
    Mar 31, 2011 at 4:29
  • but you're getting an error relating to the difference between a Song and a String. Easier if yoy create a no-arg constructor in the Song so that the String can be converted to a Song, and then set on the Singer Mar 31, 2011 at 4:33
  • Okay, I've got it now. That's what I needed. Thank you.
    – user636859
    Mar 31, 2011 at 4:36
1

Kinda hard to determine here without your Spring configuration, but looking at the code and the stack trace it appears that you're doing setter based injection but haven't provided any getter and setter methods. So you can either a) Switch to using constructor based injection or b) provide getters and setters for all of the properties you want to set via Spring.

1
  • I've tried to add some getters and setters above, but it doesn't look like it worked.
    – user636859
    Mar 31, 2011 at 4:30
1

NotWritablePropertyException : Exception thrown on an attempt to set the value of a property that isn't writable, because there's no setter method.you check all the attribute of the class which the exception provoke and do getters & setters

0

From the JavaDoc for NotWritablePropertyException

Exception thrown on an attempt to set the value of a property that isn't writable, because there's no setter method. In some situations alternatives are presented.

You don't have setters or getters.

2
  • I've tried to add some getters and setters above, but it doesn't look like it worked.
    – user636859
    Mar 31, 2011 at 4:31
  • Now it's telling you you have the wrong type: Cannot convert value of type [java.lang.String] to required type [com.springinaction.springidol.Song] for property 'song' Looks like you're trying to inject a String, not a Song. Perhaps you should post the offending xml. Mar 31, 2011 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.