Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

java.lang.NullPointerException: null on AutoWiring a bean with AnnotationConfigApplicationContext  RSS feed

 
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying to use @AutoWire feature with one of StandAlone Application unable to do so instead getting Null Pointer Exception. Please highlight my mistakes if any. Your help is appreciated.

Spring Ver 5.1.5.RELEASE and we're not using any xml config file to tell spring there are annotated classes to look into instead using @ComponentScan or @EnableAutoConfiguration at the top of AppConfig and boost strap the Context from main() class as a first line. But Autowiring works perfectly with internal bean/java classes of jdk(Environment) but not with custom POJO classes. If we're trying to get through getBean method then it works. But I'm trying to avoid creating context everywhere and using getBean() Please Refer below and help me only with your valuable guidelines.  



   

   

 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me to edit the subject my post is not getting noticed by any one. Thanks.
 
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So youre getting a null when its trying to inject rxxxblahhelper?  Does that class even exist?  Also great class names .  Class good and it extends class good11.  ;p
 
Al Hobbs
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you arent supposed to have injection code in the class. that defeats the purpose of doing dependency injection in the first place
 
Al Hobbs
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also. How did the even compile?  Looks like youre calling a method in the middle of a class
 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time to help me and to your questions.

Al Hobbs wrote:So youre getting a null when its trying to inject rxxxblahhelper?  Does that class even exist?  Also great class names .  Class good and it extends class good11.  ;p

Nice to hear you enjoy the class names  I named, because you are good as they .  Yes, There're classes exist. And they are available from ApplicationContext but unable to do Auto wiring since it throws NPE.

1. The actual code is not pasted here and the  above code is only for references as a sample code.  You are right   Looks like you are calling a method in the middle of a class actually we're not calling code in the mid of class but only in the mid of a method as below.
 
 

2. Code is compiled fine. So,

arent supposed to have injection code in the class. that defeats the purpose of doing dependency injection in the first place

I agree. In this situation, how can we utilize spring feature
to get this injected from the container with Auto Wiring ? Through this what I'm trying to achieve is, certain annotated classes/beans that're available in the spring container can be used directly from container into another classes without using new keyword.

3. Using AnnotationConfigApplicationContext and get this initialized when the main() starts so that all the beans which are annotated with @Component can available for the injection. Again, I would like to state this is not web app it's A stand alone app which provides data to some web API end point.

Hope I'm providing adequate information. But I'm stuck at analyzing the technical feasibility and seeking for help.

 
Al Hobbs
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make sure you are scanning the right packages.  Also make sure your rxxxblahhelper class is annotated with @component.   Since its not a webapp you need to make sure the spring container is started from main
 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:You need to make sure you are scanning the right packages.  Also make sure your rxxxblahhelper class is annotated with @component.   Since its not a webapp you need to make sure the spring container is started from main



Yes, I double and triple checked @Component annotation is implemented and here is the blocking point actually looking for help.  Container is started, confirmed this by logging some message as "Context Invoked" and there is no issue with that.  The thing is when we try to get the bean with

getApplicationContext().getBean(rxxxblahhelper .class);

container gives back the bean. If try two to autowire no luck with null. I'm trying to get the bean autowired and get the bean required all over the application, instead implementing context everywhere and using getBean() method.
 
Rancher
Posts: 3941
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where is instance of Good used that appears in the NullPointerException stacktrace?
Where is that instance instantiated/injected?

Can you show that code?
 
Al Hobbs
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was just about to ask that.  You need to have Good class instance created by the container too for it to work.
 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Where is instance of Good used that appears in the NullPointerException stacktrace?
Where is that instance instantiated/injected?

Can you show that code?



We call this Good from other class by reflection but the Good is also initiated at the time of component scanning as a base package.  Thanks
 
Dave Tolls
Rancher
Posts: 3941
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Sardar. wrote:
We call this Good from other class by reflection but the Good is also initiated at the time of component scanning as a base package.  Thanks



How do you get it by reflection?
I'm not sure that Spring would be involved in that, and so you would not be getting the Spring Bean, but something else entirely, something which would not have had anything injected into it.
 
Al Hobbs
Ranch Hand
Posts: 277
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thats your problem right there.  Unless youre using reflection to get the good instance made by spring (doubt it) you arent gonna get an instance with injected dependencies.
 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Mohammed Sardar. wrote:
We call this Good from other class by reflection but the Good is also initiated at the time of component scanning as a base package.  Thanks



How do you get it by reflection?
I'm not sure that Spring would be involved in that, and so you would not be getting the Spring Bean, but something else entirely, something which would not have had anything injected into it.



Sorry I think  we're not on track because Spring getBean provides all the beans that are mentioned in the componentScan but not when autowiring. So I wish to thanks Spring comes into picture here. And also, For the question asked where "Good" class is initialized  I answered by reflection but I tried passing that class name itself I could not achieve Autowiring.
 
Dave Tolls
Rancher
Posts: 3941
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a little confused here.

Can you show the call to importDoc that causes the NullPointerException?
Can you also show where in that class (the one calling importDoc) that the instance of Good it uses comes from?

For example:
 
Mohammed Sardar.
Ranch Hand
Posts: 209
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:I'm getting a little confused here.

Can you show the call to importDoc that causes the NullPointerException?
Can you also show where in that class (the one calling importDoc) that the instance of Good it uses comes from?

For example:




Yes, Sure we are missing track. But importDoc is a public method available inside Good class so we don't need create an instance. But inside importDoc, we are trying use a bean or instance that is created by AnnotationConfigApplicationContext and here is where we're getting NPE only if I use Autowired but able to use the bean if I get through Context i.e getBean(RxxxDyyyyHelper.class). Hope this adds more information.
As illustrated below with sample please read comments.






Hope this clarifies. Let me know if I miss something here.




 
Dave Tolls
Rancher
Posts: 3941
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the actual code?
Not some cut-down "it looks something like this" version, as I expect something to have gone missing.

If Autowiring had failed then I would expect a NullPointer to occur when you try and use the "good" object because it would be null.
Since getApplicationContext works then I would expect autowiring to work.

So something else is going on, and that something else is going to quite likely be in your code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!