• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Spring TaskScheduler and Integration

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this TaskScheduler that has 5 jobs being triggered by integration.

For testing purposes what I did was create a simple java class that retrieves data using MyBatis mappers.

Here is the code



Then at the main class, Im currently doing a Java Project but this is really a dynamic web project but for testing purposes.



I ran the code this exception appear at the console



For some reason every time the project begins a transaction with the database it produces an npe. Any ideas why?

Thanks,
Jet
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NPE is not when it begins the transaction. The NPE is happening on line 22 of BlahBlah. At least that's what the stack trace tells me. What is on line 22? One of the references used on line 22 has got to be null.
 
Jethro Torres
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:The NPE is not when it begins the transaction. The NPE is happening on line 22 of BlahBlah. At least that's what the stack trace tells me. What is on line 22? One of the references used on line 22 has got to be null.



Sorry didn't put the whole line of code, but for your reference here it s:



added console message.

 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that's not right. You have copy pasted your code wrong. Line 22 is just constructing an object of type email template. That line cannot throw NPE. Don't make me look at line 22, because of the way you copy pasted, the line numbers might have changed. Also, that doesn't help you learn how to solve the problem for yourself. Look at line 22 in your own IDE. That will point you to the right place

Generally, any time you get a stack trace, you should go to the exact line that threw the exception and try to figure out why it threw that exception. This is no different. An NPE simply means you tried to call a method on a reference that is null. You need to find out what variable is null in this case.
 
Jethro Torres
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:No that's not right. You have copy pasted your code wrong. Line 22 is just constructing an object of type email template. That line cannot throw NPE. Don't make me look at line 22, because of the way you copy pasted, the line numbers might have changed. Also, that doesn't help you learn how to solve the problem for yourself. Look at line 22 in your own IDE. That will point you to the right place

Generally, any time you get a stack trace, you should go to the exact line that threw the exception and try to figure out why it threw that exception. This is no different. An NPE simply means you tried to call a method on a reference that is null. You need to find out what variable is null in this case.



Sorry, about that my bad. Updated my reply with the console message it's actually having a npe exception at line 23 : emailTemplate=emailTemplateService.selectTemplateByCode(6);

But it's not supposed to have an exception since an email template with an email code of 6 exists in the database and the method I'm using is working since I'm using it on a another class and it seems to work fine there.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you eliminate everything that is impossible, whatever remains, however improbable, must be the truth.

Now, in that sentence, there are 3 things happening.
1) 6 is passed as a parameter
2) select template by code method on email template service is called
3) reference to the object returned by the method is stored in email template


It is impossible for 1) and 2) to throw NPE? So, it has to be 2) the only way 2) I will throw an NPE will be when emailTemplateService is null. So, it has to be null, however improbable you think that might be. If you like you can verify this by debugging the code, or by adding a log before line 23.


So, the question is why is it null. Well, that reference is autowired. You'll have to figure out from the sprig configuration. Has to be so,e reason why spring wouldn't autowired it.
 
Jethro Torres
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Once you eliminate everything that is impossible, whatever remains, however improbable, must be the truth.

Now, in that sentence, there are 3 things happening.
1) 6 is passed as a parameter
2) select template by code method on email template service is called
3) reference to the object returned by the method is stored in email template


It is impossible for 1) and 2) to throw NPE? So, it has to be 2) the only way 2) I will throw an NPE will be when emailTemplateService is null. So, it has to be null, however improbable you think that might be. If you like you can verify this by debugging the code, or by adding a log before line 23.


So, the question is why is it null. Well, that reference is autowired. You'll have to figure out from the sprig configuration. Has to be so,e reason why spring wouldn't autowired it.



Thanks for the advice finally found my mistake. Removed the @Autowired annotation on the service and injected the service impl into the application-context.xml. Then used the .getBean method on my class so I can finally get my data.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!