0
votes

Hi i am new for Spring and present i am running my application using Maven

but when i run my project i am getting exception like below

'year' of bean class [com.ensis.spring.MovieLister]: Bean property 'year' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Main:

public class Main {

    public static void main(String[] args){

        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        MovieLister movieLister = (MovieLister)ctx.getBean("lister");
        movieLister.printMoviesByYear();
    }
}

Movie:

private int year;
    private String name;

    public Movie(int year, String name) {
        super();
        this.year = year;
        this.name = name;
    }

    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

MovieFinder:

public class MovieFinder {

    private static List<Movie>moviesList;

    static{

        moviesList.add(new Movie(1990, "A"));
        moviesList.add(new Movie(1991, "B"));
        moviesList.add(new Movie(1992, "C"));
        moviesList.add(new Movie(1993, "D"));
        moviesList.add(new Movie(1994, "E"));
        moviesList.add(new Movie(1995, "F"));
    }

    public List<Movie>findMoviesByYear(int year){

        List<Movie>findMovies=new ArrayList<Movie>();

        for(Movie movie:moviesList){
            if(year==movie.getYear()){
                findMovies.add(movie);          
                }
        }

        return findMovies;
    }

}

MovieLister:

public class MovieLister {

    private int year;
    private MovieFinder movieFinder;


    public void printMoviesByYear(){

        List<Movie>found = movieFinder.findMoviesByYear(year);
        for(Movie movie:found){
            System.out.println("So movies are======>"+movie.getName());
        }
    }
}

applicationContext.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="lister" class ="com.ensis.spring.MovieLister" autowire="byType">
   <property name="year" value="1991"/>
   </bean>
   <bean id="finder" class="com.ensis.spring.MovieFinder"/>
   </beans>
1
please try to add getter and setter methods for year in MovieLister class and check - Avinash Reddy
Hi @avinash readdy java.lang.NullPointerException at com.ensis.spring.MovieFinder.<clinit>(MovieFinder.java:12) - Krish
now i am geeting above exception - Krish
where are you intializing your private static List<Movie>moviesList ? - Avinash Reddy
thanks @avinash redyy now i found problem - Krish

1 Answers

0
votes

Add a setter to MovieLister:

public void setYear(final int year) {
  this.year = year;
}

This is required because member variable year is private.

Side note: If a MovieLister instance requires a year or any other member variable I strongly suggest to create a constructor that sets these variables.