• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Why is my Spring @Autowired field null?

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using @Autowired for using dependency of a different class but getting NPE. My code is a simple java spring code

Code :

Output :
HI
Exception in thread "main" java.lang.NullPointerException
at test.MainClass.main(MainClass.java:24
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

@Autowired only works on fields of bean instances. It does not work on static fields. It also does not work if you create bean instances yourself (using new). You need to let both the bean, and the object you want autowired, be managed by Spring. In your example Spring isn't even involved yet.
 
R Vicky
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob, if I remove the static from @Autowired, it gives "Cannot make a static reference to the non-static field obj"

Below is the method withField() that I want to invoke to log messages in key value pair. COuld you please correct what changes I need to make. Thanks in advance.

 
Rob Spoor
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R Vicky wrote:@Rob, if I remove the static from @Autowired, it gives "Cannot make a static reference to the non-static field obj"


You need to:
* Create a Spring context.
* Get an instance of MainClass from the context.
* Access the instance's field.

I'm not sure if your KeyLogger can actually be auto-wired. It's not a Spring bean, and even if it were, it does not have a constructor that Spring can call. For each bean constructor, Spring must be able to find proper values for the parameters. Either because they are annotated (e.g. @Value), or because it's another Spring bean.
How are you even planning on creating KeyLogger instances?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you might want to start by looking at a basic Spring program, few example project tutorials I found are listed below:

https://www.tutorialspoint.com/spring/spring_hello_world_example.htm
https://www.vogella.com/tutorials/SpringDependencyInjection/article.html
https://dzone.com/articles/dependency-injection-in-spring

There are multiple ways to make Spring work, java config, xml config, component-scan to automatically find the beans (or a combination of these). Dependency injection can also be explicit or auto-wiring. You should start with whatever approach you find easiest to understand first.
 
Saloon Keeper
Posts: 26532
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:
I'm not sure if your KeyLogger can actually be auto-wired. It's not a Spring bean, and even if it were, it does not have a constructor that Spring can call. For each bean constructor, Spring must be able to find proper values for the parameters. Either because they are annotated (e.g. @Value), or because it's another Spring bean.
How are you even planning on creating KeyLogger instances?



As far as I can recall, Spring doesn't care. Although the ideal components for Spring are JavaBeans (and especially POJOs), I'm pretty sure that any class that Spring can instantiate via newInstance() can be served by the BeanFactory.

So you can't autowire completely static stuff, but with proper Spring configurations, any instantiable class should be fair game.
 
Bartender
Posts: 2264
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, R Vicky,
Maybe, put @Component to annotate KeyLogger class.
Also, make sure you have @ComponentScan in your "runner" class , the one with the main method.
@ComponentScan  is to scan the packages and sub packages of the "runner" class.
Make sure your KeyLogger is in the same package as the runner class or in the subpackages.

The reason why I suggest this because there should be a KeyLogger  bean in order to autowire it.
By annotating KeyLogger as @Component, the Spring container will create a bean out of KeyLogger class.
Reference:
https://www.baeldung.com/spring-component-scanning
 
reply
    Bookmark Topic Watch Topic
  • New Topic