Mythul
Mythul

Reputation: 1807

First time XML user, what does this error mean?

It's the first time I'm using XML in java and I would like some assistance with my code. I can't see where I'm wrong. thanks.

What I am trying to do is understand the Spring Framework.

Here is the code:

public class Student
{

    public Student(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    // VARIABLES DECLARATION
    private String name = null;
}

public class Controller
{
    public Controller(Student student)
    {
        this.student = student;
    }
    public void PrintName()
    {
        System.out.println(""+student.getName());
    }

    public Student getStudent()
    {
        return student;
    }

    public void setStudent(Student student)
    {
        this.student = student;
    }   
    // VARIABLES DECLARATION
    private Student student;
}



public class LearnXML
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        ApplicationContext factory = new ClassPathXmlApplicationContext("Student.xml");
        Controller contr = (Controller) factory.getBean("application");
        contr.PrintName();
    }

    public void setController(Controller controller)
    {
        this.controller = controller;
    }   
    private Controller controller;
}

My XML file:

    <?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.xsd">

    <bean id="student" class="domain.Student">
        <constructor-arg value="Ovidiu"   />
    </bean>
    <bean id="brain" class="controller.Controller">
        <constructor-arg ref="student"/>
    </bean>
    <bean id="application" class="learnxml.LearnXML">
        <property name="controller" ref="brain"/>   
    </bean>

</beans>

My errors:

    Dec 25, 2012 5:34:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd0099: startup date [Tue Dec 25 17:34:02 EET 2012]; root of context hierarchy
Dec 25, 2012 5:34:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Student.xml]
Dec 25, 2012 5:34:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5ceeb4a3: defining beans [student,brain,application]; root of factory hierarchy
Exception in thread "main" java.lang.ClassCastException: learnxml.LearnXML cannot be cast to controller.Controller
    at learnxml.LearnXML.main(LearnXML.java:25)
Java Result: 1

Upvotes: 0

Views: 136

Answers (4)

Brian Roach
Brian Roach

Reputation: 76908

It means exactly what it says:

learnxml.LearnXML cannot be cast to controller.Controller

You have a LearnXML object:

<bean id="application" class="learnxml.LearnXML">
    <property name="controller" ref="brain"/>   
</bean>

And are trying to cast it to a Controller object:

Controller contr = (Controller) factory.getBean("application");

getBean() is returning an Object rather than a specific type due to the inherent nature of the task at hand; it's using reflection to instantiate any type from the xml so it doesn't know what the type is at compile time. At runtime if you attempt to cast that Object to something it's not ... you get that exception.

Edit to Add:

In case it isn't obvious, it appears you're really wanting to do:

LearnXML lXML = (LearnXML) factory.getBean("application");

Upvotes: 4

NPKR
NPKR

Reputation: 5496

Change your code below line

LearnXML learnXML= (LearnXML) factory.getBean("application");

Upvotes: 1

Renjith
Renjith

Reputation: 3274

Controller contr = (Controller) factory.getBean("application"); You cant typecast LearnXML object to Controller object

Try this. LearnXML learnXml= (LearnXML ) factory.getBean("application"); learnXml.getController().PrintName();

Upvotes: 1

someone
someone

Reputation: 6572

You are getting here factory.getBean("application");

LearnXML type object but you are casting it to Controller

If you want to get Controller use brain like this

Controller contr = (Controller) factory.getBean("brain");

Upvotes: 1

Related Questions