Why is my Spring @Autowired field null?

0 votes

I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try to use it. The logs show that both the MileageFeeCalculator bean and the MileageRateService bean are being created, but I get a NullPointerException whenever I try to call the mileageCharge method on my service bean. Why isn't Spring autowiring the field?

Dec 27, 2018 in Java by Sushmita
• 6,820 points
809 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

If you are not coding a web application, make sure your class in which @Autowiring is done is a spring bean. The spring container won't be aware of the class which we might think of as a spring bean. We have to tell the Spring container about our spring classes.

This can be achieved by configuring in appln-contxt or the better way is to annotate class as @Component and please do not create the annotated class using new operator. Make sure you get it from Appln-context as below.

@Component
public class MyDemo {


    @Autowired
    private MyService  myService; 

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            System.out.println("test");
            ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
            System.out.println("ctx>>"+ctx);

            Customer c1=null;
            MyDemo myDemo=ctx.getBean(MyDemo.class);
            System.out.println(myDemo);
            myDemo.callService(ctx);


    }
    public void callService(ApplicationContext ctx) {
        // TODO Auto-generated method stub
        System.out.println("---callService---");
        System.out.println(myService);
        myService.callMydao();

    }

}
answered Dec 27, 2018 by Daisy
• 8,020 points

Related Questions In Java

0 votes
1 answer

Autowired field pointing NULL in Spring

The field annotated @Autowired is null because ...READ MORE

answered Jun 6, 2018 in Java by avi
• 160 points
2,613 views
0 votes
1 answer

How to tell Jackson to ignore a field during serialization if its value is null?

To suppress serializing properties with null values ...READ MORE

answered Jul 4, 2018 in Java by Rishabh
• 3,520 points
1,288 views
0 votes
1 answer

Sorted array is being processed faster than unsorted array. Why is it so ?

What is Branch Prediction? Consider a railroad junction: Image by ...READ MORE

answered Apr 18, 2018 in Java by Rishabh
• 3,520 points
62 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,320 points
197 views
0 votes
1 answer

Bean life cycle in Spring Bean Factory Container

Bean life cycle in Spring Bean Factory ...READ MORE

answered Aug 29, 2018 in Java by code.reaper12
• 3,450 points
170 views
0 votes
1 answer

How to download a file from spring controllers?

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void ...READ MORE

answered Sep 4, 2018 in Java by code.reaper12
• 3,450 points
67 views
0 votes
1 answer

Understanding Spring @Autowired usage

TL;DR The @Autowired annotation spares you the need ...READ MORE

answered Nov 30, 2018 in Java by Sushmita
• 6,820 points
168 views
0 votes
3 answers

How autowiring works in Spring?

First, and most important - all Spring ...READ MORE

answered Jan 11 in Java by developer_1
• 3,220 points
38 views
0 votes
2 answers

What is null in java?

"null" is a reference type and its ...READ MORE

answered Jul 31, 2018 in Java by Mrunal
• 680 points
18 views
0 votes
1 answer

What is @ModelAttribute in Spring MVC?

@ModelAttribute refers to a property of the ...READ MORE

answered Dec 18, 2018 in Java by Daisy
• 8,020 points
79 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.