Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Spring autowired variable is null when accessed from another class

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Spring autowired variable is null when accessed from another class

    Here's the problem
    I have a class A

    A.java
    public class A {
     
    	private Properties properties = new Properties();
     
    	private File file;
     
    	public File getFile() {
    		return file;
    	}
     
    	public void setFile(File file) {
    		this.file = file;
    	}
     
    	public void init() {
    		try{
    			properties.load(new FileInputStream(file));
    		}catch(IOException e){
     
    		}
    	}
     
    	public String getValue(String key) {
    		return properties.getProperty(key);
    	}
    }
    Here's how I insantiate an instance of this class.

    test-beans.xml
    ...
    <bean id="a"
    		class="com.example.A" init-method="init">
    		<property name="file" value="sample.properties"/>
    	</bean>
    ...

    There is a class that contains few common methods to be used by a number of other classes

    CommonMethods.java
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/test-beans.xml")
    public class CommonMethods {
     
    	@Autowired
    	private A a;
     
     
    	public String getFileName() {
                    if(a==null)
                         System.out.println("a is null");
                    else
                         System.out.println("a is null");
    		String s=a.getFile().getName();
    		return(s);
    	}
     
             @Test // JUnit test case
              public void testmethod() {
                  getFileName();
              }
     
    }

    There is a test class from which I need to call getFileName()

    test.java
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/test-beans.xml")
    public class test {
     
    CommonMethods cm = new CommonMethods();
     
    @Test
    public void testSomething() {
    cm.getFileName();
     
     
    }

    Now here's the problem
    When I run testmethod() from CommonMethods.java, I get a is not null.
    When I run testSomething() from test.java, I get a is null.

    Why is that? Is there a way I can use getFileName() in test.java? Please help!!


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Spring autowired variable is null when accessed from another class

    Quote Originally Posted by sakura View Post
    Now here's the problem
    When I run testmethod() from CommonMethods.java, I get a is not null.
    When I run testSomething() from test.java, I get a is null.

    Why is that?
    Because, I can guess (but I am quite sure it's this), because in test class the CommonMethods object is instantiated by you and not by Spring.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Spring autowired variable is null when accessed from another class

    I am not 100% sure if this will work, because i dont have a test environment with me to help you out.

    But, can you instantiate A before executing getFileName() methid and then check if everything works fine.

    I would strongly recomend to use Mockito in such cases. Things can become very simple and easy writing codes.
    Thanks and regards,
    Sambit Swain

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spring autowired variable is null when accessed from another class

    Thank you. I made the following changes:-

    test-beans.xml
    ...
    <bean id="a"
    		class="com.example.A" init-method="init">
    		<property name="file" value="sample.properties"/>
    	</bean>
     
           <bean id="cm"
    		class="com.example.CommonMethods">
    		<property name="a" ref="a"/>
    	</bean>
    ....

    CommonMethods.java
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/test-beans.xml")
    public class CommonMethods {
     
    	@Autowired
    	private A a;
     
           public A geta() {
                  return(a);
             }
     
              public void seta(A a) {
                   this.a=a;
              }
     
    	public String getFileName() {
                    if(a==null)
                         System.out.println("a is null");
                    else
                         System.out.println("a is null");
    		String s=a.getFile().getName();
    		return(s);
    	}
     
             @Test // JUnit test case
              public void testmethod() {
                  getFileName();
              }
     
    }

    test.java
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/test-beans.xml")
    public class test {
     
    CommonMethods cm;
     
    @Test
    public void testSomething() {
    cm.getFileName();
     
    }

    Still getting the same issue. Help me!! What am I doing wrong?

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. Replies: 149
    Last Post: February 19th, 2013, 09:04 PM
  3. Spring - property not autowired
    By timothyd in forum Web Frameworks
    Replies: 0
    Last Post: September 7th, 2011, 07:04 AM
  4. Diff between spring created been and instance of class
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 12:54 AM
  5. Post variable set to non-null vlaues
    By ss7 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2009, 11:04 AM