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

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktrace so that they remain readable... Also post full stacktraces not snippets....

    Comment


    • #3
      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)

      Comment


      • #4
        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...

        Comment


        • #5
          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.

          Comment


          • #6
            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).

            Comment


            • #7
              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");

              Comment


              • #8

                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...

                Comment


                • #9
                  Can you explain me why JDK6 and not JDK7?

                  Features of JDK6 will also be available in JDK7!

                  Comment


                  • #10
                    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..

                    Comment


                    • #11
                      Tried with JDK6....but no use.......

                      Comment


                      • #12
                        Can you post your project (zip it and attach it leave out the jar files but list them)...

                        I don't really get the error in the init block...

                        Comment


                        • #13
                          Attachment [QUOTE=Marten Deinum;426825]Can you post your project (zip it and attach it leave out the jar files but list them)...



                          The Jar file used were as follows:

                          aspectjrt-1.5.4.jar
                          aspectj-tools-1.0.6.jar
                          aspectjweaver-1.5.4.jar
                          commons-logging-1.1.1.jar\commons-logging-1.1.1.jar
                          antlr-runtime-3.0.1.jar\antlr-runtime-3.0.1.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.aop-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.asm-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.aspects-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.beans-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.context.sup port-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.context-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.core-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.expression-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.instrument. tomcat-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.instrument-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.jdbc-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.jms-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.orm-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.oxm-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.test-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.transaction-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.web.portlet-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.web.servlet-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.web.struts-3.1.2.RELEASE.jar
                          spring-framework-3.1.2.RELEASE\dist\org.springframework.web-3.1.2.RELEASE.jar
                          Attached Files

                          Comment


                          • #14
                            Runs without problems on my machine... (Although I changed it to a maven project).

                            One thing I notice is you have conflicting aspectj versions (1.5.4 and 1.0.6 ?!).

                            Comment


                            • #15
                              Originally posted by Marten Deinum View Post
                              Runs without problems on my machine... (Although I changed it to a maven project).

                              One thing I notice is you have conflicting aspectj versions (1.5.4 and 1.0.6 ?!).
                              I think aspectj jar files were not necessary to run this program.

                              I remover these jar files and recompiled the program, but still the same problem........

                              thanks for taking interest to solve this problem..

                              Comment

                              Working...
                              X