title: Spring 3 @Autowired Annotation Issues
body: <p>I've been taking a look at some tutorials for getting started with the Spring Framework and came across <a href="http://pookey.co.uk/wordpress/archives/50-getting-started-with-maven-and-spring" rel="nofollow">this one</a>. After following it through, I ran into the errors below. (I have since updated to use version 3 of the spring framework and I am still getting the same errors.</p>
  
  <p>In short, I am getting a NullPointerException where I call a method on an AutoWired class.</p>
  
  <p>My Class is SayHello:</p>
  
  <pre><code>public class SayHello {
  
  private String name;
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getName() {
      return name;
  }
  
  public void greet() {
      System.out.println("Hello " + getName());
  }
  
  }
  </code></pre>
  
  <p>I then set the following in /src/test/resources/applicationContext.xml.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
   &lt;context:annotation-config/&gt;
  
  
    &lt;bean id="hello" class="package.SayHello"&gt;
      &lt;property name="name" value="John Smith" /&gt;
    &lt;/bean
  </code></pre>
  
  <p></p>
  
  <p>And finally I have the AppTest class where I try to AutoWire a SayHello object to test.</p>
  
  <pre><code>public class AppTest {
  
  @Autowired
  private SayHello hello;
  
  @Test
  public void testApp()
  {
      hello.greet();
      Assert.assertTrue( true );
  }
  }
  </code></pre>
  
  <p>When I try to run AppTest I get the following error:</p>
  
  <pre><code>java.lang.NullPointerException
  at package.AppTest.testApp(AppTest.java:19)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:601)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>I've had a look at various articles and questions on stackoverflow but after trying a few and not getting anywhere, thought I'd create a new post.</p>
  
  <p>Cheers.</p>
  
  <p>If it helps, heres the dependencies that I'm using (Spring v3.2.1) :</p>
  
  <pre><code>&lt;dependencies&gt;
  
  &lt;!-- JUnit --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;4.11&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  
  
  &lt;!-- Spring --&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Update</strong></p>
  
  <p>I've also tried using the @Autowired annotation in the main application as follows. And still get a NullPointerException error.</p>
  
  <pre><code>public class App 
  {   
      @Autowired
      private static SayHello hello;
  
      public static void main( String[] args )
      {
         hello.greet();
      }
  }
  </code></pre>