Varun
Varun

Reputation: 4452

Error creating bean in Spring

Hi all i am a beginner for spring i just started it. i am getting an error

" Error creating bean with name 'question' defined in class path resource [org/collection/ApplicationContext2.xml]: 3 constructor arguments specified but no matching constructor found in bean 'question' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)"

i have two class 1st is Question that contains a single constructor Question and the second class is Answer

i am trying to create reference of answer class and insert into the Question class which have Array List

i goggled it and found that i need to specify the type. i have already specified it but still i am getting the error

Thanks..

Question.java

package org.collection;

import java.awt.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;



public class Question {
    private int id;
    private String name;
    private ArrayList<String> answers;
    //private HashSet<String> answers1;

    public Question()
    {
        //Default constructor
    }

    public Question(int id,String name,ArrayList<String> answers)
    {
        super();
        this.id=id;
        this.name=name;
        this.answers=answers;

    }

    public void display()
    {
        System.out.println("Id :"+id+"\nName :"+name);
        System.out.println("Answers are");
        Iterator<String> itr= answers.iterator();  
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }

        /*System.out.println("----------picking up the answers from HashSet---------");
         Iterator<String> itr1=answers1.iterator();
         while(itr1.hasNext())
         {
             System.out.println(itr1.next());
         }
         System.out.println("-------reached-----------");*/



    }

}

Answer.java

package org.collection;

public class Answer {
    private int id;
    private String name;
    private String by;

    public Answer() {
        // TODO Auto-generated constructor stub
    }

    public Answer(int id,String name,String by)
    {
        super();
        this.id=id;
        this.name=name;
        this.by=by;

    }

    public String toString()
    {
        return "ID :"+id+"\nName"+name+"\nBy :"+by;
    }
}

ApplicationContext.xml2

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

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

 <bean id="ans1" class="org.collection.Answer">
 <constructor-arg value="1" type="int"></constructor-arg>
 <constructor-arg value="java is a progamming language hahahaha"  type="java.lang.String"></constructor-arg>
 <constructor-arg value ="varun"  type="java.lang.String"> </constructor-arg>
 </bean>
 
 <bean id ="ans2" class="org.collection.Answer">
 <constructor-arg value="2" type="int"></constructor-arg>
 <constructor-arg value="java is a platfornm"  type="java.lang.String"></constructor-arg>
 <constructor-arg value ="Rahul"  type="java.lang.String"></constructor-arg>
 </bean>
  
 <bean id="question" class= "org.collection.Question">
 <constructor-arg value="111" type="int"></constructor-arg>
 <constructor-arg value="What is java ?" type="java.lang.String"></constructor-arg>
 <constructor-arg>
 <list>
 <ref bean="ans1"/>
<ref bean="ans2"/> 
 </list>
  </constructor-arg>
  </bean>
 </beans>

Upvotes: 0

Views: 1926

Answers (1)

Mithun
Mithun

Reputation: 8077

The three parameter constructor in Question is expecting List of String. But, you are passing List of Answer. Change the third parameter in Question class to ArrayList<Answer> answers

Upvotes: 2

Related Questions