Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: NullPointer Exception while loading bean file through ClassPathXmlApplicationContext

  1. #1

    Exclamation 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 at 01:51 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Please use [ code][/code ] tags when posting code/xml/stacktrace so that they remain readable... Also post full stacktraces not snippets....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Code:
     package com.tutorialspoint;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
    
    	public static void main(String[] args) {
    		
    		ApplicationContext context = new ClassPathXmlApplicationContext("Beans-config.xml");
    		
    		HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    		
    		obj.getMessage();
    		
    	}
    	
    }
    Code:
    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); 
    	} 
    }
    Code:
    <?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/schema/beans/spring-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>
    Code:
    Oct 05, 2012 2:39:28 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35389244: startup date [Fri Oct 05 14:39:28 IST 2012]; root of context hierarchy
    Exception in thread "main" java.lang.ExceptionInInitializerError
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
    	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    	at com.tutorialspoint.MainApp.main(MainApp.java:10)
    Caused by: java.lang.NullPointerException
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Which spring version? Doesn't it work or does it simply log this message and continues... Please try to be a bit more informative else it will be guessing...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    I'm using spring-framework-3.1.0.M2 version.

    When i try to compile the program it's displaying NullPointerException.

    What i understood is..ApplicationContext is unable to find the bean file. The bean file is located under ../src folder.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Why are you using a milestone (i.e. beta) version instead of a final..

    It has nothing to do with compilation you are already trying to start the application... The NullPointer, in general, shouldn't come from the fact that it cannot load the file (then you would get another exception). Also the exception occurs in a static initializer and not whilst loading the file.

    Also make sure you are on java6 not 5 and for starters upgrade to spring 3.1.2 instead of the in between milestone version (and update your xsd in your file to 3.1 or remove the version altogether).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    I upgraded to spring 3.1.2.

    I'm using jdk1.7.0_02 and jre7.

    Still i'm getting the same problem.

    it's showing error in MainApp.java in 10th line

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

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default


    it's showing error in MainApp.java in 10th line
    No it's not, check again it is in the init block of DefaultListableBeanFactory...

    Try JDK6 instead of 7, could be a problem...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    Can you explain me why JDK6 and not JDK7?

    Features of JDK6 will also be available in JDK7!

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    The fact that features are available doesn't matter, there are several changes in JDK7 which might break things... So just give it a try to see if it helps..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •