Autowired field pointing NULL in Spring

0 votes
Making @Service class in spring framework that has an @Autowired field which is pointing to NULL POINTER Exception while running. Any solution how to make field autowired in Spring??
Jun 6, 2018 in Java by mitto
• 160 points
14,468 views

1 answer to this question.

0 votes

The field annotated @Autowired is null because Spring doesn't know about the copy of MileageFeeCalculator that you created with new and didn't know to autowire it.

The Spring Inversion of Control (IoC) container has three main logical components: a registry (called the ApplicationContext) of components (beans) that are available to be used by the application, a configurer system that injects objects' dependencies into them by matching up the dependencies with beans in the context, and a dependency solver that can look at a configuration of many different beans and determine how to instantiate and configure them in the necessary order.

The IoC container isn't magic, and it has no way of knowing about Java objects unless you somehow inform it of them. When you call new, the JVM instantiates a copy of the new object and hands it straight to you--it never goes through the configuration process. There are three ways that you can get your beans configured.

Check this sample example

public class MileageFeeController {

    @Autowired
    private MileageFeeCalculator calc;

    @RequestMapping("/mileage/{miles}")
    @ResponseBody
    public float mileageFee(@PathVariable int miles) {
        return calc.mileageCharge(miles);
    }
}


That should do your job!

Enroll in Spring Framework Course online to learn more about it.

Thanks!

answered Jun 6, 2018 by Avi
• 160 points

edited Mar 4, 2022 by Sarfaraz

Related Questions In Java

0 votes
1 answer

Why is my Spring @Autowired field null?

If you are not coding a web ...READ MORE

answered Dec 27, 2018 in Java by Daisy
• 8,140 points

edited Mar 4, 2022 by Sarfaraz 18,780 views
+1 vote
0 answers

Initially autowired object has value,it becomes null after paging(using JSF 2.2 and spring 4.01.)

i'm trying to create basic crud operations ...READ MORE

Jul 5, 2019 in Java by swats
1,652 views
0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,500 points
907 views
0 votes
1 answer

How can we add JTable in JPanel with null layout?

The JPanel should have some layout manager. JTable ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,640 points
1,423 views
0 votes
1 answer

Annotation wiring in Spring

By default, Annotation wiring is not turned ...READ MORE

answered Jul 4, 2018 in Java by code.reaper12
• 3,500 points
1,144 views
0 votes
1 answer

Bean Scopes in Java Spring

According to my knowledge, the Spring Framework ...READ MORE

answered Jul 4, 2018 in Java by geek.erkami
• 2,680 points
1,189 views
0 votes
1 answer

How do I fix a NullPointerException?

When you declare a reference variable (i.e. ...READ MORE

answered Apr 13, 2018 in Java by Parth
• 4,640 points
931 views
0 votes
1 answer
0 votes
1 answer

@Component vs @Repository vs @Service in Spring Framework

As you might be knowing, all these ...READ MORE

answered May 29, 2018 in Java by geek.erkami
• 2,680 points
3,651 views
0 votes
1 answer

What are some of the important Spring annotations which you have used?

Some of the Spring annotations that I ...READ MORE

answered Feb 18, 2019 by Frankie
• 9,830 points
521 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP