Announcement Announcement Module
Collapse
No announcement yet.
NullPointer Exception while loading bean file through ClassPathXmlApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointer Exception while loading bean file through ClassPathXmlApplicationContext

    Hi,

    I'm a beginner to spring. I'm getting a nullpointer exception while loading the bean class.

    I loaded all the required spring jars.

    Can any help me to solve this problem.


    Project structure:

    HellpSpring
    -src
    --com.tutorialspoint
    ---HelloWorld.java
    ---MainApp.java
    --Beans-config.xml

    Thank you!



    Main file
    -----------

    package com.tutorialspoint;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class MainApp {

    public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("Beans-config.xml");

    HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

    obj.getMessage();

    }

    }
    -----
    HelloWorld

    package com.tutorialspoint;

    public class HelloWorld
    {
    private String message;
    public void setMessage(String message)
    {
    this.message = message;
    }
    public void getMessage()
    {
    System.out.println("Your Message : " + message);
    }
    }

    ----------
    Beans-config.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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">

    <property name="message" value="Hello World!"/>

    </bean>

    </beans>

    --------

    Error log

    Oct 05, 2012 11:48:38 AM org.springframework.context.support.AbstractApplic ationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@35389244: startup date [Fri Oct 05 11:48:38 IST 2012]; root of context hierarchy
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.springframework.context.support.AbstractRefres hableApplicationContext.createBeanFactory(Abstract RefreshableApplicationContext.java:195)
    at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:128)
    at org.springframework.context.support.AbstractApplic ationContext.obtainFreshBeanFactory(AbstractApplic ationContext.java:522)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:436)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at com.tutorialspoint.MainApp.main(MainApp.java:10)
    Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.<clinit>(DefaultListableBeanFac tory.java:104)
    ... 7 more
    Last edited by avinash.madireddy; Oct 5th, 2012, 02:52 AM.

  • #2
    Please don't post duplicates and please post in the correct forums ...

    Comment

    Working...
    X